Cind programming language

Language definition Examples Download Contact

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 static, for example:

static int y = 10;

is a declaration of static integer variable y with initial value 10 (for more about variables see Variables).

Example of static variable as a class member (for more about classes see Classes):

class c { int x; // variable of class static int y; // static variable for all objects of class ... }

In above, variable x is created every time for every object of class c, but there is only one variable y shared by all of them, and it is accessible for every object of class c.

Example of static variable in a function:

fun() { int x; // variable in function context static int y; // static variable for all function instances ... }

Similar to the previous example, variable x will be created for every function call, but there will be only one variable y shared by all function calls.

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 before instruction. For example:

static { x = 2*y; .... }

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:

static int x = 10;

is equivalent to the instructions:

static int x; static { x = 10; }

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:

static int x; int y; static fun z() { // ... can access variable x, but can't y } static class c { // ... can access variable x and function z }

Cind programming language 1.0.4