Static code
Static variables
Static variable is a variable associated to some piece of code, which can be accessed by all instances that shares that code snippet.
For example, all objects of given class shares its static variables and can access them concurrently.
Declaration of static variable begins with word
is a declaration of static integer variable
Example of static variable as a class member (for more about classes see Classes):
In above, variable
Example of static variable in a function:
Similar to the previous example, variable
Static context (a set of static variables) will be freed only when given code will be removed from memory.
For instance, static variable of a class will be freed only when all objects of that class will be removed,
and class definition will be unloaded from memory.
Static sections
Static section is a piece of code executed in static context.
Syntax of the static section is a word
Static section is executed only once, by the first thread that enters the section.
Next thread will skip the static section execution, but if execution is not done yet,
then it will wait for its end.
If there will be an exception thrown during the static section execution,
then each thread that will going to pass the section, will get this exception.
Static variable initialization
Initialization of static variables is executed inside static sections.
What means, that static variable declaration, for example:
is equivalent to the instructions:
Static functions and classes
Functions and classes can be also declared as a static, which will mean,
that they can only use a static context from the place of declaration.
For example: