answersLogoWhite

0

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.

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

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.


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

difference between constant and static variables in java


Why static variables in a function have global extent?

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.


Can static variables be changed?

No, a static variable means that there is only one copy of that variable, and it is shared by all members of the class, or by all callers of a function.A variable that is read-only would be marked as const or final (depending on language).


How can you change the value of a constant variable 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.

Related Questions

What is meant by static identifier?

Nothing. I guess you mean a static variable.


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


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.


Can static variable be used inside a static method?

Yes.


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

difference between constant and static variables in java


What is the static variable in the class in c?

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


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.


Why static variables in a function have global extent?

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.


Will multiple JVMs running the same application containing a static variable hold individual copies of that static variable?

Yes


Are static variable created by giving keyword static in java?

yes bcoz static variables