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
11
votes
1 answer

Does the one-definition-rule force a single static function variable to be created?

As an example, consider this header: #include template struct A { A() { static int x; std::cout << &x << "\n"; } }; static A<> a; What if I had two different C++ files including this file - would it…
orlp
  • 112,504
  • 36
  • 218
  • 315
11
votes
4 answers

Access a global static variable from another file in C

In C language, I want to access a global static variable outside the scope of the file. Let me know the best possible way to do it. One of the methods is to assign an extern global variable the value of static variable, In file a.c static int val =…
Sikandar
  • 357
  • 2
  • 4
  • 13
11
votes
4 answers

singleton and public static variable Java

I have 2 options: Singleton Pattern class Singleton{ private static Singleton singleton = null; public static synchronized Singleton getInstance(){ if(singleton == null){ singleton = new Singleton(); } …
Achow
  • 8,600
  • 6
  • 39
  • 49
10
votes
1 answer

ARC: How to release static variable?

Will dealloc (below) release the NSString pointed to by the static variable exampleString? // ExampleClass.h @interface ExampleClass : NSObject @end // ExampleClass.m static NSString *exampleString; @implementation ExampleClass - (void)dealloc…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
10
votes
7 answers

PHP OOP - constant vs static variables?

In PHP, What is is the difference between: Constants and static variables? Extending a class and creating its object? I know how they can be used, but I can't clearly distinguish between them.
pMan
  • 8,808
  • 11
  • 32
  • 35
9
votes
4 answers

How do I change a static variables value in PHP?

This is a simplified version of what I want to accomplish: In my script I want a variable that changes true and false everytime the script is executed.
Weblurk
  • 6,562
  • 18
  • 64
  • 120
9
votes
1 answer

Static initialization order of inline variables in single TU

I'm aware this question has been asked many times, but this seems to be a slightly different variation which I can't figure out. Consider the following code: #include struct TestValue; inline const TestValue* v_ptr = nullptr; struct…
9
votes
2 answers

BroadcastReceiver Life Cycle -- Static Variables

I have a BroadcastReceiver class. I have some static variables declared whose value is updated in side the onReceive() method. As per my knowledge static variable will keep it's value across the onReceive calls. Is there any possibility when I will…
Sush
  • 6,839
  • 1
  • 18
  • 26
9
votes
3 answers

Objective C - Static and global variable?

In my .m file for a class named Ad , I have 3 static strings static NSString *AdStateDisabled = @"disable"; static NSString *AdStateExpired = @"expired"; static NSString *AdStateActive = @"active"; I can simply use these static variables in the…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
9
votes
6 answers

Static variables in static method in base class and inheritance

I have these C++ classes: class Base { protected: static int method() { static int x = 0; return x++; } }; class A : public Base { }; class B : public Base { }; Will the x static variable be shared among A and B, or…
Meh
  • 7,016
  • 10
  • 53
  • 76
9
votes
4 answers

Access of static variable from one file to another file

I recently came across the question like how to access a variable which declared static in file1.c to another file2.c? Is it possible to access static variable? My understanding about static keyword in C is, static is "internal linkage", so they are…
vinay hunachyal
  • 3,781
  • 2
  • 20
  • 31
9
votes
5 answers

Initialising a static variable in Objective-C category

I was trying to create a static variable to store a dictionary of images. Unfortunately, the best way I could find to initialise it was to check in each function that used the variable. Since I am creating this variable inside a category, I can't…
Casebash
  • 114,675
  • 90
  • 247
  • 350
9
votes
3 answers

Scala equivalent of C++ static variable in a function

I am quite new to Scala and stumbled across following problem: what is Scala equivalent of function's static variable ? void foo() { static int x = 5; x++; printf("%d", x); } EDIT: What I want to achieve is a kind of function call…
tommyk
  • 3,187
  • 7
  • 39
  • 61
8
votes
3 answers

Multiple threads reading static variable at the same time

My question may be newbie or duplicate, but i wonder what is happening when several threads try to read a static variable at the same time. I'm not interesting in synchronization now, i just want to know are they reading it instantly or by…
donRumatta
  • 836
  • 2
  • 9
  • 22
8
votes
2 answers

Are static local variables bad practice?

Related C++ question: Static local variables in methods a bad practice? In VB.NET, when I want a simple counter or something that increments each time a method is called, I often find myself writing code like: Private Sub tmrRefresh_Tick(ByVal…
Flash
  • 15,945
  • 13
  • 70
  • 98