What does a static variable mean in general for different programming languages ​​and circumstances?

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

But what does this have to do with static? What's so static about it? I thought, what static

does it mean that it doesn't change?

For example, in vb.net, static is written together, which means a member function that can be accessed without creating an object. A static function inside usually means that the variable lifetime is the lifetime of the entire program. It seems that static variables are stored in the code section of the computer. Is it correct in my understanding based on the example?

+3


source to share


8 answers


Well, I think the keyword is appropriate. This means that the variable that you declare static will remain in the same place throughout the entire execution of your program.

I thought static funds don't change



This matches the keyword const

. Const means it doesn't change, static means it doesn't "move" as it stays in the same place.

+15


source


In general, what does not change with something static in a programming language is whether it is alive or not. static variables are always alive; they have one instance, which occurs either at the beginning of the program, or the first time they are visible and continue until the end of the program. Non-static variables come and go, as blocks are injected and left, or how class instances are created and destroyed.



In C ++, for C compatibility reasons, static, when applied to variables in the scope of the namespace, has a completely unrelated meaning: this means that the variable has an internal, not external relationship, and does not appear in other translation units. Why the word static was adopted for this in the beginning of C I don't know; I can only guess that they need something and don't want to enter a new keyword. (Originally in the earliest versions of C, variables in the file scope obeyed the rules of a Fortran called a shared block: all variables with the same name refer to the same store.) Looking back, of course (from 20/20 hindsight), the default for variables in the file scope must have an internal link, with a special keyword (public

?) to say that the variable had an external link. But this was much less obvious in the early 1970s.

+6


source


static is an access specifier that restricts scope but forces the variable to exist for the life of the program. This means that a static variable is one that is not visible outside the function in which it is declared, but remains until the program ends. This also means that the value of the variable is retained between successive calls to the function. The value of such a variable will remain and can be seen even after calling the function. Another thing is that the declaration of such a variable inside a function will only be executed once.

To use, follow these steps:

void fun() { 
    static int fnvalue=0;//Executed once 
    printf(" \n%d",fnvalue++);// Value changed retains for next call 
} 

      

This code behaves like a sequence generator. Another advantage of static variables is that because they are created once and then exist for the life of the program, the address of the variable can be passed to modules and functions that are not in the same C file so that they can access the variable content. This has several programming advantages.

+4


source


Static refers to the storage of variables. Inside a function call, every variable you declare is pushed onto the stack. Unlike other variables, a static variable is not pushed onto the stack, it is like a global variable that survives throughout the execution of the program, with the difference that is visible only inside the block.

+3


source


I guess you just need to learn the meaning of "static" in computer science and not relate to speaking English. Especially since this applies to variables and functions, with slightly different results in C.

+1


source


Definition of word http://dictionary.reference.com/browse/static?s=t

  • related or characterized by a fixed or stationary state.
  • shows little or no change: static concept; static relationship.

A static variable is one that retains its state even after it goes out of scope, as opposed to a non-static variable that would be reinitialized every time it went back into scope - so one might think, " steady state "or demonstrates" no change "

+1


source


If you can avoid it, just don't go into statics for C ++. In any modern language, static simply means only one instance and is never destroyed. It's not too far from the English meaning and is good at driving the discussion about const / final / readonly and what it means.

+1


source


A static variable means that 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. If you want an example for example, we have two streams. In the first thread, you create a progress indicator, and in the second, you update it. In this case, you can define a static variable in the progressbar class to save the progress and create one instance of the class on each thread. One thread to initialize, and in the other you change the value of a static variable. Since both are using the same copy, progress will be available in the first thread. So static means something that doesn't change its location when a new instance is created. Or we can say something that preserves his state;) Blah blah blah

+1


source







All Articles