Sub topic 12
When and How Classes Are Initialized
This chapter assumes you can compile and run Java programs and understand classes, methods, static members, and exceptions. It also assumes you know that the JVM loads and links a class before executing its code.
Initialization is a separate phase
Class initialization runs the class's static field initializers and static initializer blocks. The JVM performs it after loading and linking, but only when initialization is needed.
A class is commonly initialized before:
- An instance is created with
new. - A static method is called.
- A non-constant static field is read or written.
- Reflection explicitly requests initialization.
Reading a compile-time constant does not usually initialize its class. A static final primitive or String initialized with a constant expression can be copied into the using class during compilation.
Initialization order
When the JVM initializes a class, it first initializes its superclass. It then runs the class's static field initializers and static blocks in source order.
Consider this complete program:
public class InitializationDemo {
static class Parent {
static int value = initializeValue();
static {
System.out.println("Parent block");
}
static int initializeValue() {
System.out.println("Parent field");
return 10;
}
}
static class Child extends Parent {
static {
System.out.println("Child block");
}
static int number = 20;
}
public static void main(String[] args) {
System.out.println("main");
System.out.println(Child.number);
}
}The output is:
main
Parent field
Parent block
Child block
20Child.number is an active use of Child. Therefore, the JVM initializes Parent first. Within each class, the field initializer and block run in their written order.
A class is initialized at most once. If several threads reach the first active use at the same time, the JVM synchronizes initialization. One thread runs the initialization code, while other threads wait. After successful initialization, later uses do not run the static code again.
Constants and interfaces
This example does not initialize Helper when it reads CONSTANT:
public class ConstantDemo {
public static void main(String[] args) {
System.out.println(Helper.CONSTANT);
System.out.println(Helper.value);
}
}
class Helper {
static final int CONSTANT = 42;
static int value = initialize();
static int initialize() {
System.out.println("Helper initialized");
return 7;
}
}The first read uses a compile-time constant. The second read accesses a normal static field, so it triggers initialization.
Initializing a class also initializes its superclass first. An interface is not normally initialized merely because an implementing class is initialized. Interfaces are initialized when their own non-constant static fields are actively used. Interfaces with default methods have additional initialization rules because the JVM may need to initialize them before a class.
Failure during initialization
If static initialization throws an exception, the JVM marks the class as erroneous. The first active use commonly reports an ExceptionInInitializerError. Later attempts can report NoClassDefFoundError, because initialization previously failed.
Keep static initialization simple. Validate required configuration early, avoid complex I/O in static blocks, and preserve the original cause when reporting failures.
Exercise
In what order do the static sections run when a subclass is actively used?
Show answer
The JVM initializes the superclass first, running its static fields and blocks in source order. It then initializes the subclass in source order.