I've heard there are differences between languages about the meaning of the keyword static
, but I've not found a good list that consolidates those differences.
Here's what I know about the meaning of static
in C++:
- For local static variables within a function, the variable is initialized at startup and the value is saved across function calls.
- Static data members are shared among all instances of a class. In other words, there is only one instance of a static data member. Static data members must be initialized at file scope.
- Static member functions have access only to static members.
- In recursive code, a static object or variable is guaranteed to have the same state in different instances of a block of code.
- Static objects and variables defined at file scope only have internal linkage. No other files may use them.
How does the meaning of static
change in other languages?
C
- The keyword can change either the linkage or the duration (lifetime) of an object.
- Variables are always initialized to 0
- Functions have internal linkage.
- If declared in file level scope: variables have internal linkage and static duration (i.e. exists throughout the lifetime of the program)
- If declared in block scope: variables have no linkage but static duration
- There can multiple declarations of the same static variable in a translation unit. However, note that they must be the same. E.g: at file-level scope:
int a; // a has external linkage
static int a; // a now has static linkage
// same as if you wrote: static int a = 0;
//...
static int b; // static linkage
extern int b; // extern loses its meaning, b still has internal linkage
//...
extern int b; // b has external linkage
static int b; // error
//...
void func() {
static int x; // automatic linkage, static duration
// same as if you wrote: static int x = 0;
}
C++
- At file level scope the usage has been deprecated for both variables and members in favor of anonymous namespaces. Exists only as compatibility
- Variables still get default initialized (as in C) to 0
- "6.7 The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place [...] "
- Variables have static storage duration unless accompanied by a
thread_local
specifier (from C++0x onwards)
- There can be only one definition of a static in a translation unit
- Member variables/functions mean they are properties of the class and not the instances
Legal access syntax: instance.property or Class::property
- Static member functions can only access only static member variables
No
this
pointer for such functions
- Non-static members can however access any static member
- At file level objects have internal linkage except for class members which have a class scope
- Class members need to be defined either in the class declaration or outside explicitly via class name and scope resolution operator
- Cannot use
this
in a static method
ActionScript
- Class methods as in C++
- cannot use
this
or super
in a static method
- Accessed only through class name and not instance name
- Not inherited
- Derived classes however have access to bases' static properties
- Variables that are declared with both the static and const keywords must be initialized at the same time as you declare the constant
Object Oriented Design
- The Singleton design pattern is considered by many as a glorified static object
- Used in Factory design pattern
I may have missed a lot of other things -- feel free to chip in.