Static (keyword)
static is a reserved word found in numerous programming languages, primarily used to alter declarations by influencing aspects such as lifespan (as seen with static variable) and accessibility (linkage), or to designate a class member rather than an instance-specific member.
Origin
Earlier languages that preceded C, like BCPL and B, incorporated the idea of static storage, referring to storage that persists throughout program execution. Although B did not include a static keyword, it utilized extrn for external static storage and auto for automatic storage tied to function calls. In B, all variables were declared as either auto, extrn, or function parameters.
With C, the successor to B, new storage class specifiers static and register were introduced alongside the inherited auto and extern. Additionally, C formalized the notion of linkage, enabling variables and functions to have internal linkage (restricted to their compilation unit) or external linkage (accessible throughout the entire program). These specifiers determine both the storage duration and linkage characteristics:
| Storage class | Duration | Linkage |
|---|---|---|
extern
|
static (duration of program execution) | external (visible across entire program) |
static
|
static (duration of program execution) | internal (translation unit restricted) for top-level declarations none for block-scoped variables |
auto, register
|
function call lifetime | none |
Every variable or function belongs to one of these classes; by default:
- Top-level declarations are considered
extern, - Variables within functions default to
auto.
Within C, the use of static signifies:
- For top-level declarations: internal linkage rather than external linkage for both variables and functions.
- For block-scoped variables: static storage duration instead of automatic duration.
Therefore, in C terminology, "static variable" can refer to:
- A variable with lifetime lasting the entire program run (a general concept);
- A variable explicitly declared with the storage class
static(specific to C).
Variables declared with extern, including those without explicit storage class at top-level scope, maintain static lifetime but do not necessarily possess static linkage.
In C++, the keyword static also applies to class member variables that share a single instance across all objects of that class. Such members require definition outside the class body. When applied to member functions, it indicates that they operate independently of any particular object instance—disallowing access to non-static members or the implicit this pointer.
As a storage duration specifier
The keyword static, in many programming languages, marks a local variable so that it maintains its value throughout the entire runtime of the program, preserving state between multiple invocations of a function. This contrasts with automatic storage duration variables which are reinitialized each time the function is called. This behavior traces back to its origin in C.
Languages supporting local variables declared as static include C, C++, Objective-C, C#, and PHP. Conversely, languages with syntax similar to C but lacking support for this feature include Java, JavaScript, and Dart, where persistent variables must be defined outside function scopes.
As a visibility modifier
At the top-level in C, the static keyword restricts the visibility of a variable or function to within its own compilation unit. While contemporary programming languages generally utilize namespaces to avoid naming conflicts, this particular role of static remains primarily in C-derived languages such as C++, and Objective-C. Other languages apply distinct keywords for controlling visibility, like public in Java or internal/file in C#.
As a class member modifier
In the majority of class-oriented programming languages, the static keyword is used to designate members that belong to the class itself rather than any individual instance, following the pattern established by C++.
Characteristics of static members include:
- They exist independently of any object instances and are accessed through the class name.
- Their associated fields persist for the entire duration of the program.
- Static methods cannot interact with non-static members unless provided with an instance reference.
Certain languages expand the use of static within classes:
- Java and JavaScript offer static initializers that execute once when the class is loaded.
- C# provides static constructors that run prior to any usage of the class, along with static classes that cannot be instantiated.
- In Java, marking an inner class as
staticremoves its implicit link to an outer class instance, allowing it to be used independently. - The
statickeyword facilitates class-level polymorphism (commonly known as late static bindings) by enabling dynamic referencing of static members.
As a closure specifier
In certain programming languages, applying static to anonymous functions prevents them from capturing external state, which differs from their default behavior. This concept is analogous to static local variables and static class members since a static closure does not depend on the current invocation context of its enclosing function.
- PHP: When closures are declared inside instance methods, they automatically bind to the
$thisobject; usingstaticdisables this binding. - C#: Closures normally capture all accessible local and instance variables; marking them as
staticensures no external state is enclosed.