Tag Info

The static keyword, in object-oriented programming languages, is used to define a function/method or field/property as bound to the context of a type/class, but not to any specific instance. Unlike constants, static properties can usually be changed at runtime. static also refers to the storage class in languages like C. static variables are initialized once, at run-time, and persist in between function calls (i.e. they aren't re-initialized).

# static in OO context

static methods, are methods defined in the context of a type, or class, that can be invoked without having to create an instance of that type. As a result, the method has access to hidden (protected or private) properties and methods. A static method allows for a number of patterns (like Singleton, Factory and Registry to name a few).

Static properties are properties that, though bound to the context of a given type (or Class), do not reside in the memory that was allocated for an instance. Instead, they reside in a global storage area, which each instance accesses. As a result, if the value is changed via one instance, or (if the property is public) directly through the type definition, all instances will reflect that change.

      some static field
/       |      \
/        |       \
instance1   |        instance3
instance2


A simple example, using pseudo-code:

//assign using instance1
instance1::someStaticField = 1;
//increment using instance2
instance2::someStaticField++;
//get value using instance3
for(i=0;i<instance3::someStaticField;i++)
{
the loop body will be executed 2 times, the value is 2 for all instances
}


The static keyword is found in many of the well-know, widely used design patterns like the Singleton, Factory or Service Locator patterns. It's also used as a means to store global values (eg configuration data), that need to be available throughout the code-base, without having to resort to global variables. The component containing this data is often referred to as the Registry.

Special case: static class
C# allows the programmer to define a class as a static class. These classes are meant to bundle related functions together, to be called freely. A static class cannot be instantiated. Their main use-cases are to create fire-n'-forget methods, and to avoid having too many (often redundant) methods lying around. However, static class's come at a cost, use with care. Moderation is key.

# static storage class

The keyword static is also used in C, as a storage class specifier. The default storage class for global variables is the static storage class. This means that these global statics will be visible to all functions within that source file, but not to any other modules that may be brought in at link time. Local variables can also be defined as being static. This means that these variables will be initialized at runtime, and not be re-initialized each time the function is called. Their value remains static throughout.

Example 1 - static global variable

#include <stdio.h>
static int foo = 123;
const char *bar = "Global, too";//or even without static
int main ( void )
{
void another_func( void );
printf("I can see foo = %d\n, bar is %s\n", foo, bar);
++foo;
another_func();
another_func();
return 0;
}
void another_func( void )
{
static i = 0;
++i;
printf("I can see foo = %d\n, bar is %s\nI was called %d times\n", foo, bar,i);
}


The output will look like this:

I can see foo = 123
bar is Global, too
I can see foo = 124
bar is Global, too
I was called 1 times
I can see foo = 124
bar is Global, too
I was called 2 times


So all functions can see/use the global statics, and if at any point their value is changed, then that change will be noticed by all other functions, too (obviously). A static variable in a function (as i in this case) retains its value in between calls, but cannot be accessed from outside that function's scope.