answersLogoWhite

0


Best Answer

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

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

A static variable is a variable that exists for the entire duration of a program. Compare with a non-static variable that is only instantiated when it comes into scope and is released from memory when it falls from scope.

Static variables are often confused with global variables, however static variables can be declared local to functions and classes -- they are only regarded as being global when they are actually declared with global scope.

Static variables must always be initialised at compile time. In the case of static variables within functions, you declare and initialise the variable just as you would declare and initialise any non-static variable, except the declaration is prefixed with the static keyword. The only real difference is that static variables are initialised at compile time, not every time the function is called. Once initialised, the function is free to modify the value and the value will be maintained between calls to that function.

A typical usage of a static variable that is local to a function is to determine if a function has already been called or not. This ensures the function is only executed once per run. For instance:

void foo()

{

static bool beenhere = false; // Initialised at compile time.

if( beenhere ) return;

else beenhere = true;

// Any code placed here executes just once....

}

A static variable declared in a function is local to that function -- it cannot be accessed directly from outwith the function. However, the variable exists at all times, even when the function is not in scope.

Classes can also declare static member variables. These variables are shared amongst all instances of the class and, since they exist for the entire duration of the program, they are also accessible even when no instance of the class exists. Public static variables are accessible outwith the class while private and protected static members are only accessible to the class itself, including any static methods of the class and friends of the class.

Since static member variables exist even when no instance of the class exists, they cannot be initialised from within the class (which would require an instance of the class). Initialisation is typically done at file scope from within the implementation file of the class, and outwith the methods of the class:

// Header file: foo.h

class foo{

public:

foo(){};

public:

static int static_var;

};

// Implementation file: foo.cpp

#include "foo.h"

int foo::static_var = 0; // Initialised at compile time.

// Source file: main.cpp

#include <iostream>

#include "foo.h"

int main()

{

printf( "Static: %d\n", foo::static_var );

return( 0 );

}

In the above example, the static variable is public and is accessible even though no instance of foo actually exists.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

there are 3 main uses for the static. 1. If you declare within a function: It retains the value between function calls 2.If it is declared for a function name: By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files 3. Static for global variables: By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file. by mayank shukla there are 3 main uses for the static. 1. If you declare within a function: It retains the value between function calls 2.If it is declared for a function name: By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files 3. Static for global variables: By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file. by mayank shukla

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A static field or method is a keyword to indicate that: A) For a class member, the memory address of the member for all instances of this class is shared. B) For a class method, an instance of this class is not needed to be called.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.

While programming you might have observed that there are many classes creating constants that can be read, without creating an object.

For example:

static final int VERSION = 2;

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Static Variables and functions are parts of a class that do not require an object of the containing class to be invoked or accessed. They are not specific to every instance of the class's object.

For ex: let us say there is a class name StaticExample that contains a variable named NAME and a method named getDefaultDate() you can access them as:

String name = StaticExample.NAME;

java.util.Date date = StaticExample.getDefaultDate();

You need not instantiate an object of the class StaticExample to access these values.

Note: Static methods can access only static variables. They cannot access or use class level instance variables.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

My answer really depends on your skill level in programming. For a newer programmer, i would say that a static variable is a variable that works for everything. You put it first in your code and it is capable of running through all parts of your program.

For a more advanced programmer, a proper definition would be that a static variable is a variable whose lifetime extends across the entire run of the program. Static is not a term used in all coding (such as Pascal), where global variables and local variables take that part.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Static variables are common to all objects of that class. They are associated with the class (rather than the object) and is located in a fixed location in memory. Changes to the value of the static variable is visible to all objects.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A static variable is a variable that remains in memory throughout the life-time of the program. The variable may not always be in scope, however it's value is retained across scopes. Static variables are allocated in static memory within the program's data segment.

Static functions are functions that are local to the translation unit in which they are defined. This provides a means of encapsulating functions and thus prohibiting access to those functions via external code (code that resides outwith the translation unit).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What does it mean when a variable is static?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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. &lt;?php function fun() { static $variable; static $another_variable = ''; } ?&gt;


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.


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.


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.


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.


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