Sub topic 8
The Class Loading Lifecycle
Assume you can compile and run Java programs and understand classes, methods, static members, and exceptions. This chapter follows one class from its binary name to its first usable execution.
The lifecycle at a glance
Before the JVM can execute a method, it must make the class available and prepare its runtime representation. The lifecycle has three broad stages:
- Loading reads a class file or another byte source and creates a
Classobject. - Linking checks and prepares the class. It includes verification, preparation, and usually resolution.
- Initialization runs static field initializers and static initializer blocks.
The stages are related, but they are not always completed all at once. The JVM can delay resolution until it needs a particular symbolic reference.
Loading
A class loader receives a binary name such as com.example.Report. It locates bytes for that class and asks the JVM to create a class definition from them. The JVM records the defining class loader as part of that definition.
Loading does not run static code. For example, reading Report.class with a class loader does not necessarily execute Report's static initializer.
The loader may throw ClassNotFoundException when an explicit request, such as Class.forName, cannot find the requested class. This is a checked exception because the caller can often recover by choosing another class or configuration.
Linking
Linking prepares the loaded definition for execution.
- Verification checks that the class file is structurally valid and that its bytecode obeys JVM rules. This protects the runtime from malformed or unsafe class files.
- Preparation allocates and assigns default values to static fields. An
intfield receives0, a reference receivesnull, and abooleanreceivesfalse. Constant variables may receive their final constant values during preparation. - Resolution changes symbolic references, such as “method
savein typeStore,” into direct runtime references. The JVM may perform this step lazily.
Linking can fail before any application static initializer runs. A missing referenced type may produce a NoClassDefFoundError, depending on when and how the missing type is encountered.
Initialization
Initialization runs only when the JVM determines that active use requires it. Typical triggers include:
- Creating an instance with
new. - Invoking a static method.
- Reading a non-constant static field.
- Starting a class with
java.
The JVM initializes a superclass before its subclass. Within one class, static field initializers and static initializer blocks run in source order.
public class Startup {
static int count = setCount();
static {
System.out.println("block");
}
static int setCount() {
System.out.println("field");
return 10;
}
public static void main(String[] args) {
System.out.println(count);
}
}The output is:
field
block
10Initialization is coordinated per class. If multiple threads reach an uninitialized class, one thread performs initialization while the others wait. If initialization fails, the JVM records the failure. The first attempt commonly reports ExceptionInInitializerError; later attempts often report NoClassDefFoundError.
A practical debugging view
Use unified logging to observe loading:
java -Xlog:class+load=info MyApplicationUse Class.forName("com.example.Report") when you need to request loading and initialization explicitly. The one-argument overload initializes the class; the three-argument overload lets you choose whether initialization occurs.
Exercise
Which phases occur when a class is loaded, and which phase runs static initializers?
Show answer
Loading creates the class definition, linking verifies and prepares it, and initialization runs its static fields and initializer blocks.