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
229
votes
6 answers

How can I access "static" class variables within methods?

If I have the following code: class Foo(object): bar = 1 def bah(self): print(bar) f = Foo() f.bah() It complains NameError: global name 'bar' is not defined How can I access class/static variable bar within method…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
161
votes
2 answers

Superiority of unnamed namespace over static?

How are unnamed namespaces superior to the static keyword?
Nawaz
  • 353,942
  • 115
  • 666
  • 851
119
votes
12 answers

What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?

I have searched about static variables in C#, but I am still not getting what its use is. Also, if I try to declare the variable inside the method it will not give me the permission to do this. Why? I have seen some examples about the static…
Kartik Patel
  • 9,303
  • 12
  • 33
  • 53
80
votes
6 answers

C++ static initialization order

When I use static variables in C++, I often end up wanting to initialize one variable passing another to its constructor. In other words, I want to create static instances that depend on each other. Within a single .cpp or .h file this is not a…
Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
73
votes
4 answers

What's the meaning of static variables in an implementation of an interface?

I don't quite understand static variables when defined in the implementation of an interface. In methods I do understand how they differ from local variables, but not when defined directly in an implementation. Look at these examples. What…
quano
  • 18,812
  • 25
  • 97
  • 108
72
votes
10 answers

When should I use static methods in a class and what are the benefits?

I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need to call a method of a class, I create an object of that class and call the…
Naveed
  • 41,517
  • 32
  • 98
  • 131
56
votes
4 answers

What makes a static variable initialize only once?

I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function. That is cool, but how is that implemented? Does it translate to some kind of twisted if statement? (if given a…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
51
votes
5 answers

C++ static member variable and its initialization

For static member variables in C++ class - the initialization is done outside the class. I wonder why? Any logical reasoning/constraint for this? Or is it purely legacy implementation - which the standard does not want to correct? I think having…
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72
51
votes
3 answers

c#: static variable in a static method

Can you have a static variable in a static method? Would the value of this variable be preserved across all calls to the method? eg. public static void MyMethod() { static int x = 0; x++; }
Craig Johnston
  • 757
  • 2
  • 8
  • 9
32
votes
8 answers

Value of static variable not changed even after initializing the child class in Java

When I invoke the static variable y by using Checks.y (Checks being a subclass), the static block is not executed and the value of y doesn't get updated. class Par { static int y = 4; } class Checks extends Par { static { y = 5; …
tusharRawat
  • 719
  • 10
  • 24
32
votes
2 answers

What is the difference between .LIB and .OBJ files? (Visual Studio C++)

I know .OBJ is the result of compiling a unit of compilation and .LIB is a static library that can be created from several .OBJ, but this difference seems to be only in the number of units of compilation. Is there any other difference? Is it the…
25
votes
2 answers

Access static variable from static method

I want to access a static variable from a static method: #!/usr/bin/env python class Messenger: name = "world" @staticmethod def get_msg(grrrr): return "hello " + grrrr.name print Messenger.get_msg(Messenger) How to do it…
Dmitry Isaev
  • 3,888
  • 2
  • 37
  • 49
24
votes
10 answers

In C, does using static variables in a function make it faster?

My function will be called thousands of times. If i want to make it faster, will changing the local function variables to static be of any use? My logic behind this is that, because static variables are persistent between function calls, they are…
24
votes
5 answers

Is it OK to use static variables to cache information in ASP.net?

At the moment I am working on a project admin application in C# 3.5 on ASP.net. In order to reduce hits to the database, I'm caching a lot of information using static variables. For example, a list of users is kept in memory in a static class. The…
Vincent McNabb
  • 33,327
  • 7
  • 31
  • 53
23
votes
1 answer

Is a static variable in a library (DLL) shared by all processes referencing that library?

I know that a static variable used in a web application is shared for all users across the web application. If I have a library (DLL) that uses some static private variable, do all applications using that library share the value of that…
Devin Burke
  • 13,642
  • 12
  • 55
  • 82
1
2 3
53 54