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
15
votes
5 answers

Static variables in C#

In C#, is there a way to put a static variable in a method like VB.Net? Static myCollection As Collection
Nick O
  • 3,716
  • 6
  • 38
  • 50
15
votes
3 answers

Can I change static variable initialization order in C++?

I use C++ in Visual Studio 2015 sp3. By #pragma init_seg(compiler) , I initialize some static variables first(to memory management). https://msdn.microsoft.com/en-us/library/7977wcck.aspx But, there is #pragma init_seg(compiler) in…
P-P
  • 1,670
  • 5
  • 18
  • 35
15
votes
3 answers

How to access a variable across two files

I have three files - global.php, test.php, test1.php Global.php $filename; $filename = "test"; test.php $filename = "myfile.jpg"; echo $filename; test1.php echo $filename; I can read this variable from both test and test1 files by include…
user2688512
  • 161
  • 1
  • 1
  • 5
14
votes
3 answers

What is the proper way to declare static variables in Objective-C?

Ok, still re-adjusting to things when switching between C, C++, C# and Objective-C so sometimes my head spins. This time however, I'm more confused as to the proper way since I have seen at least three different ways to declare static variables in…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
14
votes
1 answer

Static class members python

So I'm using static class members so I can share data between class methods and static methods of the same class (there will only be 1 instantiation of the class). I understand this fine, but I'm just wondering when the static members get…
Falmarri
  • 47,727
  • 41
  • 151
  • 191
14
votes
3 answers

Where java static variables are stored in memory?

class A{ static int i = 10; static int j = 20; static void getname(){ } } Where will these variable be stored in memory ?
Deepak
  • 167
  • 1
  • 1
  • 9
13
votes
1 answer

Why does this static const int member variable appear to be accessible publicly in array definition?

I make the following declarations: class Servo { protected: static const int maxServos = 16; static Servo servos[maxServos]; //Array declaration }; Servo Servo::servos[Servo::maxServos]; //Array definition ...and it compiles, which…
13
votes
4 answers

Javascript local static variable

Not sure I completely understand answers to similar questions that I found here, so trying to be absolutely sure: I would like to have a local variable in a function, initialized only once (similar to static variables in strongly-typed languages…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
13
votes
6 answers

Making a superclass have a static variable that's different for each subclass in c#

Without any code in the subclasses, I'd like an abstract class to have a different copy of a static variable for each subclass. In C# abstract class ClassA { static string theValue; // just to demonstrate public string GetValue() { …
ste
  • 818
  • 2
  • 9
  • 16
12
votes
5 answers

How do you clear a static variable in PHP after recursion is finished?

So for example, I have a static variable inside a recursive function, and I want that variable to be static through out each call of the recursion, but once the recursion is finished, I want that variable to be reset so that the next time I use the…
trusktr
  • 44,284
  • 53
  • 191
  • 263
12
votes
5 answers

Static variable for optimization

I'm wondering if I can use a static variable for optimization: public function Bar() { static $i = moderatelyExpensiveFunctionCall(); if ($i) { return something(); } else { return somethingElse(); } } I know that…
keithjgrant
  • 12,421
  • 6
  • 54
  • 88
12
votes
5 answers

Static variables in instance methods

Let's say I have this program: class Foo { public: unsigned int bar () { static unsigned int counter = 0; return counter++; } }; int main () { Foo a; Foo b; } (Of course this example makes no sense since I'd…
Davide Valdo
  • 779
  • 8
  • 21
12
votes
2 answers

Static block variable in Objective-C

Is it possible to have a static variable of a "block type"? I have a class that only does stuff in static methods. Upon execution of those methods i'm calling statusChangedBlock. Just for that i create a shared instance of the class, and use its…
user1244109
  • 2,166
  • 2
  • 26
  • 24
11
votes
4 answers

android static variable behaviour on application crash

In my application I have Loginactivity. It has a static variable username and it will be assigned with the user enter values of username. Loginactivity launch activity A and A launch B. In A i use the variable Loginactivity.username. Now due to some…
png
  • 4,368
  • 7
  • 69
  • 118
11
votes
1 answer

static variable lifetime and application pool recylcing

I understand the lifetime of static variables in relation to applications (console/windows) but I'm not sure if I am understanding their lifetime when in the context of web apps (asp.net, mvc, web api, etc). From what I understand, when IIS recycles…
Mike Johnson
  • 686
  • 5
  • 13
1 2
3
53 54