What does static variable in general mean for various programming language and circumstances?

18,956

Solution 1

Well, I think the keyword is appropriate. It means the variable you declare as static will remain stored at the same location throughout the whole execution of your program.

I thought static means doesn't change

This corresponds to the const keyword. Const implies it doesn't change, static implies it doesn't "move", as to it stays stored at the same location.

Solution 2

In general, what doesn't change with something that is static in a programming language is whether it is alive or not. Static variables are always alive; they have a single instance which comes into being either at the beginning of the program or the first time they are visible, and lasts until the end of the program. Non-static variables come and go, as blocks are entered and left, or as class instances are created and destroyed.

In C++, for reasons of C compatibility, static, when applied to variables at namespace scope, has a completely unrelated meaning: it means that the variable has internal, rather than external linkage, and is not visible in other translation units. Why the word static was adopted for this in early C, I don't know; I can only guess that they needed something, and didn't want to introduce a new keyword. (Originally, in the very earliest versions of C, variables at file scope obeyed the rules of a Fortran named common block: all variables of the same name referred to the same storage.) Looking back, of course (with 20/20 hindsight), the default for variables at file scope should have been internal linkage, with a special keyword (public?) to say that the variable had external linkage. But this was a lot less obvious in the early 1970's.

Solution 3

Static is referred to the variable storage. Inside a function call, every variable that you declare is pushed on the stack. Unlike other variables, a static variable isn't pushed on the stack, it's like a global variable, that survives the whole execution of the program, with the difference that is visible only inside the block is declared.

Solution 4

Static variable means ,there is only one copy of the variable,even if you create multiple instances of the class.That is, all objects of the specified class use the same memory location.Or if you want an example,say , we have two threads .On first thread you create a progressbar and on the second you need to update it.In this case you can define a static variable in your progressbar's class to store the progress and create one instance of the class in each thread.One thread for initialising and in the other you change the value of static variable.Since both use the same copy the progress will be available in the first thread. So static means something that doesnt change its location on creating a new instance..Or we can say something tha preserves its state ;) Blah blah blah

Solution 5

I think you just have to learn the meaning of "static" in computer science, and not relate it to spoken English. Especially as it applies to variables and functions, with slightly different outcomes in C.

Share:
18,956
user4951
Author by

user4951

Updated on June 27, 2022

Comments

  • user4951
    user4951 almost 2 years

    Static variables are usually: (in most programming languages) shared, persistent, and allocated on the code section of the program

    But what does that have anything to do with the word static? What is so static about that? I thought static means doesn't change?

    For example, in vb.net static is written shared and that means a member function that can be accessed without object instantiation. Static within function usually means that the variable life time is the life time of the whole program. It seems that static variables are stored on the code section of the computer. Am I correct in my understanding based on the example?

  • James Kanze
    James Kanze about 11 years
    Any instance of a variable will be stored at the same location for as long as the variable lives. The key meaning behind static is that there is no coming and going of the variable.
  • user4951
    user4951 about 11 years
    That's the essence of this question. How do I relate the static concept in computer science with spoken english.
  • user1725145
    user1725145 over 9 years
    That's the point of my answer - there is no gain by relating the two.