Questions tagged [static-variables]

In object oriented programming, a static variable is a variable which belongs to the class and not to object(instance) & a single copy to be shared by all instances of the class.

In object oriented programming, a static variable is a variable which belongs to a class rather than to a particular instance of the class.

799 questions
8
votes
6 answers

In C++, what happens if two different functions declare the same static variable?

void foo() { static int x; } void bar() { static int x; } int main() { foo(); bar(); }
Andrew
  • 1,590
  • 4
  • 19
  • 25
8
votes
4 answers

What actually compiler does when we declare static variables?

I want to know what actually going under the hood,how compiler treats static variables. Unlike auto variable, static variable's value persist even after the end of block but how compilers actually handle this?
8
votes
1 answer

Main Program and Shared Library initializes same static variable in __static_initialization_and_destruction_0

Does anyone know why a library initialized within dlopen() would initialize a static variable owned by the main program. Both the main program and shared library have a copy of the static variable, but for some reason the shared library…
8
votes
2 answers

NullPointerException with static variables

I just hit very strange (to me) behaviour of java. I have following classes: public abstract class Unit { public static final Unit KM = KMUnit.INSTANCE; public static final Unit METERS = MeterUnit.INSTANCE; protected Unit() { } …
tomekK
  • 748
  • 8
  • 19
8
votes
5 answers

Can I have different copies of a static variable for each different type of inheriting class

I want to have the same static variable with a different value depending on the type of class. So I would have public class Entity { public static Bitmap sprite; public void draw(Canvas canvas, int x, int y) { …
Glen
8
votes
4 answers

Can static function access non-static variables in php?

var1; echo "
".self::$staticVar ."
"; …
user1983198
  • 111
  • 1
  • 1
  • 2
8
votes
2 answers

Can I free() static and automatic variables in C?

The code is as follow : #include int num = 3; // Static external variable int *ptr = # int main(void) { int num2 = 4; // Automatic variable int *ptr2 = &num2; free(ptr); //Free static variable free(ptr2); //Free automatic…
caramel1995
  • 2,968
  • 10
  • 44
  • 57
7
votes
1 answer

Is there a way to change a local typed constant from *outside* the routine it's declared in?

Please note that this is just a thought experiment. I know global (static) vars are bad and breaking scope is a bad idea in any case. Consider the following code: function IsItChanged: integer; const CanIBeChanged: integer = 0; begin Result:=…
Johan
  • 74,508
  • 24
  • 191
  • 319
7
votes
1 answer

pyspark udf print row being analyzed

I have a problem inside a pyspark udf function and I want to print the number of the row generating the problem. I tried to count the rows using the equivalent of "static variable" in Python so that when the udf is called with a new row, a counter…
roschach
  • 8,390
  • 14
  • 74
  • 124
7
votes
2 answers

Is a mutex defined statically in a function body able to lock properly?

Is a mutex defined statically in a function body able to lock properly? I am currently using this pattern in my logger system, but I have not tested it's thread safety yet. void foo () { static std::mutex mu; std::lock_guard
user9854078
7
votes
3 answers

problem creating object of inner class in java

Here is the code. public class Test { class InnerClass{ } public static void main(String[] args){ InnerClass ic = new InnerClass(); } } It says the error message non-static variable this cannot…
bunkdeath
  • 2,348
  • 5
  • 21
  • 23
7
votes
2 answers

Referencing a possibly destroyed static object

Assuming I have the following code Something.hpp #pragma once class Something { public: static Something& get(); private: Something(); }; Something.cpp #include "Something.hpp" #include using namespace std; Something&…
Curious
  • 20,870
  • 8
  • 61
  • 146
7
votes
1 answer

'... incorrectly extends base class static side' error when overriding static field in derived class

Overriding static field in derived class causes error TS2417: Build:Class static side 'typeof TDerived' incorrectly extends base class static side 'typeof TBase'. Is this a legit error case? class TBase { private static s_field = 'something'; …
dodo951
  • 438
  • 4
  • 8
7
votes
8 answers

What exactly does "static" mean when declaring "global" variables in Java?

I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on. public class Member { // Global Variables int…
Hristo
  • 45,559
  • 65
  • 163
  • 230
7
votes
4 answers

Update static variables in java

I have a class with static variables as: class Commons { public static String DOMAIN ="www.mydomain.com"; public static String PRIVATE_AREA = DOMAIN + "/area.php"; } And if I try to change DOMAIN from an Android Activity (or another java…
AlexBerry
  • 103
  • 1
  • 1
  • 6