Question:
What is a static variable in simple terms?
randomhappyface
2011-01-14 19:35:30 UTC
I am taking computer science in grade 10 and yeah... what is a static variable?
Four answers:
SAM
2011-01-14 19:40:02 UTC
Class variable, also known as static variable, are those that belong to the class. So, the memory allocation and initialization for these variables doesn't depend the object of the class. The class variables will be created and will be initialized when the class is loaded into the memory. The will be only one memory chunk for the class variables and all object (that are instantiated for this class) will share the class variable and access the same memory location to get the value for the class variable. Since these class variable are not dependent on the objects, they can be accessed by simply using the class name. For example,



ClassName.CLASS_VARIABLE_NAME



Hope this helps!!!



Regards

SAM
Techwing
2011-01-15 04:16:38 UTC
It depends somewhat on which programming language you are using, but in general, "static" means that the variable doesn't move during the life of the program. In languages like C, ordinary variables are given memory space when a procedure starts to run, and then the memory space is given up when the procedure finishes. A static variable, however, has memory allocated to it permanently within the program, and stays in the same place for as long as the program is running, no matter what procedures and functions are executed.



In some other languages, static can mean that the variable is actually a constant and cannot be changed. The word "static" itself means "not changing," so anything called static either stays in the same place for the entire duration of a program and/or never changes its value or contents.



The other answers you see here apply only to certain languages, particularly C and C++. But there are many languages, so static can have slightly different meanings in other languages, as described above.
2011-01-15 03:53:20 UTC
A variable lives only during its scope. So a variable in a function only exists when that function is running. Run the function again and the value of the variable is whatever it is at declaration. (Usually a numeric variable is 0, a Boolean is false and a string is "".)



A static variable retains its value from one running of the function to the next. It's sort of a global variable that can only be accessed from within the function.
SALEM M
2011-01-15 12:41:45 UTC
A static variable as opposed to dynamic variable is a variable that remains available until the program is running given the scope of its declaration:



- Static global variable: it is visible and accessible by all sub-modules of the main program.



- Local static variable: declared in a subprogram (procedure or function) that is visible and accessible locally and whose life is limited is the time of the execution of that procedure or function.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...