When are static variables initialized?
Solution 1:
From See Java Static Variable Methods:
- It is a variable which belongs to the class and not to object(instance)
- Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables
- A single copy to be shared by all instances of the class
- A static variable can be accessed directly by the class name and doesn’t need any object.
Instance and class (static) variables are automatically initialized to standard default values if you fail to purposely initialize them. Although local variables are not automatically initialized, you cannot compile a program that fails to either initialize a local variable or assign a value to that local variable before it is used.
What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded.
In case of inner classes, they can not have static fields
An inner class is a nested class that is not explicitly or implicitly declared
static
....
Inner classes may not declare static initializers (§8.7) or member interfaces...
Inner classes may not declare static members, unless they are constant variables...
See JLS 8.1.3 Inner Classes and Enclosing Instances
final
fields in Java can be initialized separately from their declaration place this is however can not be applicable to static final
fields. See the example below.
final class Demo
{
private final int x;
private static final int z; //must be initialized here.
static
{
z = 10; //It can be initialized here.
}
public Demo(int x)
{
this.x=x; //This is possible.
//z=15; compiler-error - can not assign a value to a final variable z
}
}
This is because there is just one copy of the static
variables associated with the type, rather than one associated with each instance of the type as with instance variables and if we try to initialize z
of type static final
within the constructor, it will attempt to reinitialize the static final
type field z
because the constructor is run on each instantiation of the class that must not occur to static final
fields.
Solution 2:
See:
- JLS 8.7, Static Initializers
- JLS 12.2, Loading of Classes and Interfaces
- JLS 12.4, Initialization of Classes and Interfaces
The last in particular provides detailed initialization steps that spell out when static variables are initialized, and in what order (with the caveat that final
class variables and interface fields that are compile-time constants are initialized first.)
I'm not sure what your specific question about point 3 (assuming you mean the nested one?) is. The detailed sequence states this would be a recursive initialization request so it will continue initialization.
Solution 3:
Static fields are initialized when the class is loaded by the class loader. Default values are assigned at this time. This is done in the order than they appear in the source code.
Solution 4:
The order of initialization is:
- Static initialization blocks
- Instance initialization blocks
- Constructors
The details of the process are explained in the JVM specification document.