Quantcast
answersLogoWhite
notificationBell
Java Programming
C Programming
C++ Programming

What does static variable mean?


Top Answer
User Avatar
Wiki User
Answered 2015-09-21 11:26:31

A static variable is a variable that is allocated at compile time and that remains in memory for the entire duration the program remains in memory. Contrary to the misleading answer given below, static variables ARE variables, they are NOT constants; the value is initialised at compile time but can be modified at runtime whenever the variable is within scope. Static constants are also possible, but they must be explicitly marked constant -- otherwise they are implicitly variable.

As with ordinary variables, the scope of a static variable is dependant upon where it is declared. This could be at global scope, file scope, function scope, namespace scope or class scope. However, the scope only determines the visibility of a static variable. The variable exists at all times even when not in scope so its value can persist across scopes, but the value can only be modified when the variable is currently in scope.

Global scope means the static variable has external linkage and is accessible to all code with access to the definition. File scope limits visibility to the translation unit that defines it. Function scope limits visibility to the function that defines it. Namespace scope limits visibility to the namespace that defines it, but you can still gain external access via the scope resolution operator (::).

Class scope is the most complex scope because as well as scope resolution, the variable is also subject to the class access specifier (public, protected and private) associated with it. However, another key difference is that static class member variables are scoped to the class itself, not to any instance of the class. Thus they are visible (if not accessible) even when no instances of the class exist.

***************PREVIOUS ANSWER*********************

A common misconception about and misuse of the static qualifier is:

A static variable is program variable that does not vary... go figure.

Static variables have the same value throughout run time. They can be changed at design time only.

This is actually a better description of the const modifier.

'static' is used in C programs to declare a variable that should exist throughout the lifetime of the program. When the program starts executing, all allocations are made for static variables. It has the added side-effect of making the variable not visible outside the scope of the module in which is declared. The explanation in the answer below this one describes this well.

For more information specific to java see: http://mindprod.com/jgloss/static.html Answerstatic is an access qualifier that limits the scope but causes the variable to exist for the lifetime of the program. This means a static variable is one that is not seen outside the function in which it is declared but which remains until the program terminates. It also means that the value of the variable persists between successive calls to a function. The value of such a variable will remain and may be seen even after calls to a function. One more thing is that a declaration statement of such a variable inside a function will be executed only once.

For usage try following: 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.

One more advantage of static variables is that, since 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 aren't in the same C file for them to access the variable's contents. This has several advantages in programming.

For further confusions or details write me: rupesh_joshi@sify.com,rupesh.joshi@gmail.com

C++ and Java In Object-oriented languIages like C++ and Java, the static keyword has additional practical value. If you define a class-level variable as static, that variable is accessible whether there is an instance of the class or not. Furthermore, all instances of the class share the same single value. This is used in a number of ways.

Sometimes you'll have a class where you want all the instances to share a single variable (EG a bus class where all buses in the system shared the same totalRiders variable.) Often you'll define constants as static final, because they are frequently used as parameters for the constructor of the class.

Methods can also be static, which means they can be called without an instance of the class. Most often this is used to make a class act like an old-school function library. The Java Math class is a great example of this. You never instantiate the Math class, but all of its methods are static.

A static member is almost always called with the class name rather than the instance name.
"static" in programming and databases means "constant--never changing". So, a static variable could be the "node name" or "database name". Once those are set, they cannot be changed.

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Still Have Questions?

Related Questions

What is meant by static identifier?

Nothing. I guess you mean a static variable.


What is the difference between static variable and global variable declaration?

When you say static variable, do you mean local static variable or global static variable? In C, the difference between global static variables and global variables is that static in this case means that the variable can be used only in the module (.c file) that it is declared. The difference between a local static variable and a global variable is the scope: the local variable can be used only inside the function that declares it. So you can have 2 local static variables in the same file with the same name but in different functions and they will be 2 different variables.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


Scope of static variables?

Scope of static variable is with in the file if it is static global. Scope of static variable is with in the function if variable is declared local to a function. But the life time is throughout the program


What is the difference between static variable and global variable?

static variable is available to the module where it is defined. Global variable is available through out the pgm


Which one executed first static block or static variable in java?

Static variable gets initialized first and then the static block gets executed.


What is the scope of a global variable that is declared as static?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


What does it mean when a variable is static?

It means the variable is stored in the program's data segment (static memory). This memory is allocated when the program loads and remains allocated until the program terminates. This means that when the variable falls from scope, its value is not lost.


How may a static variable be changed in C?

You can change a static variable by putting the static variable into a function that has operations that you intend to execute upon the variable. Example, you want to change the static variable to another value or make an addition of 2. Put the source code inside a function with the static variable declared locally within the function.Every time you call the function, the static variable value will change. Take note that the static variable retains the last value you declared it in your function call.A more terse answerLocal variables declared as static are changed as normal; they are special in that their values persist across function calls.


How do you create static variables in PHP?

Use the "static" keyword to declare a static variable within a function like shown below. <?php function fun() { static $variable; static $another_variable = ''; } ?>


Why use static variable in programming language?

For C programming, the use of a static variable has two uses: One reason is to hide the variable from other modules. The scope of the static variable is limited to the compilation unit that it is described in. The second use of a static variable is to keep the value of the variable intact through the entire program execution unit.


What is static variable and how it is declared?

static variables are declared to define a variable as a constant., means if you declare a variable as static the variable becomes costant.syntaxstatic int a=100;this will make the value of a as 100 which is not to be changedWell, no; you think of 'const', which can be used together with static, but not necessarily.Yes you are right bro I was confused it should be const int a=100; then the variable will be a constant.


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


Difference between static variable and automatic variable?

In C programming, a static variable declared outside of any function is global, but local to the current module. A static variable declared within a function retains its value between function calls.



What is a static identifier in C?

In C, a variable declared as static in a function is initialised once, and retains its value between function calls. The default initial value of an uninitialized static variable is zero. If a function or global variable is declared static, it can only be accessed in that file.


Difference between Constant Variable and Static Variable in C?

They are entirely different things, 'constant' means: not to change its value, 'static' means 'not public' and/or 'not local' variable.


Can we call static variable with this keyword?

Variables cannot be called, only functions and methods. A static variable can be accessed through the "this" keyword like any other variable.


What is static variable in the class for c plus plus?

A static member variable is local to the class rather than to an object of the class.


What is the difference between auto variable and static variable?

A static variable is declared once at the start of a program and it does not disappear when it goes out of scope. An auto variable is declared within a function or block and disappears when it goes out of scope.


Static variable in c?

A static variable in C is one in which the memory is preallocated before the execution unit begins and lasts for the entire program unit.A non-static variable in C will be allocated in the block in which it is contained, and destroyed outside that block.


What is importance of static variable?

The static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.


Can static variables be changed?

A static variable is one which is not stored on the stack but in the memory of the program. Static variables can be changed.



What is static char in C?

static storage class in C tells that: The variable will have the default value as zero. The variable scope will be the file in which it is defined. RaVi


Still have questions?

Trending Questions
How to Make Money Online? Asked By Wiki User
Best foods for weight loss? Asked By Wiki User
Does Neil Robertson wear a wig? Asked By Wiki User
Previously Viewed
What does static variable mean? Asked By Wiki User
Unanswered Questions
How old is zak beggans? Asked By Wiki User
Does arsenio hall have ms? Asked By Wiki User