Static (keyword)
This article needs additional citations for verification. (June 2022) |
This article is missing information about the use of the static keyword to declare class methods in C++ and Java.(April 2014) |
static
is a reserved word in programming languages such as C, C++, Objective-C, C#, Java, PHP, Javascript, Dart, etc., to modify a declaration. The effect of the keyword varies depending on the details of the specific programming language, most commonly used to modify the lifetime (as a static variable) and visibility (depending on linkage), or to specify a class member instead of an instance member in classes.
C
In C, the static
keyword declares a static variable, which is a variable with the same lifetime as the program. In addition, if the static
keyword is used for a top-level variable, it makes it to have internal linkage only, instead of external linkage by default.
static
is a storage class specifier (not to be confused with classes in object-oriented programming), with extern
, auto
and register
(which are also reserved words) as well. Every variable and function has one of these storage classes; if a declaration does not specify the storage class, a context-dependent default is used:
extern
for all top-level declarations in a source file,auto
for variables declared in function bodies.
Storage class | Lifetime | Visibility |
---|---|---|
extern
|
program execution | external (whole program) |
static
|
program execution | internal (translation unit only) |
auto , register
|
function execution | (none) |
In C, the term "static variable" has two meanings which are easy to confuse:
- A variable with the same lifetime as the program, as described above (language-independent); or
- (C-family-specific) A variable declared with storage class
static
.
Variables with storage class extern
, which include variables declared at top level without an explicit storage class, are static
in the first meaning but not the second.
Static global variable
A variable declared as static
at the top level of a source file (outside any function definitions) is only visible throughout that file ("file scope", also known as "internal linkage"). In this usage, the keyword static
is known as an "access specifier". It is statically allocated with the lifetime being the program execution.
Static function
Similarly, a static function – a function declared as static
at the top level of a source file (outside any class definitions) – is only visible throughout that file ("file scope", also known as "internal linkage").
Static local variables
Variables declared as static
inside a function are statically allocated, thus keep their memory location throughout all program execution, while having the same scope of visibility as automatic local variables (auto
and register
), meaning they remain local to the function. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.
C++
In C++, the keyword static
extends to member variables and functions. In the case of static member variable.
Static member variables
Member variables declared as static
inside class definitions are class variables (shared between all class instances, as opposed to instance variables). They are also statically allocated with the lifetime being the execution of the program, the same with static
top-level variables and static
local variables.
Static member function
Similarly, a static member function – a member function declared as static
inside a class definition – is meant to be relevant to all instances of a class rather than any specific instance. A member function declared as static
can be called without instantiating the class, and it cannot access the this
pointer or access non-static members of the class.
Java
This keyword static
means that this method is a class method; it will be called through class name rather than through an object.
A static method is normally called as <classname>.methodname()
, whereas an instance method is normally called as <objectname>.methodname()
.