Sub topic 3
Linking, Initialization, and Execution
Assume you can compile basic Java programs and understand classes, methods, static members, and exceptions. This chapter follows a loaded class through linking, initialization, and execution.
Linking: verification, preparation, and resolution
After loading a class file, the JVM links it:
- Verification checks that the bytecode is structurally valid and follows JVM safety rules. For example, an instruction must use the correct operand types.
- Preparation creates static fields and gives them default values such as
0,false, ornull. It does not run field initializers yet. - Resolution changes symbolic references, such as a method name and descriptor, into usable runtime references. The JVM may perform this step lazily, when the reference is first used.
caption: A class moves through linking before initialization and execution.
alt: Flow from loading to verification, preparation, resolution, initialization, and execution.
flowchart TD
A[Load class file] --> B[Verify bytecode]
B --> C[Prepare static fields]
C --> D[Resolve symbolic references]
D --> E[Initialize class]
E --> F[Execute methods]When classes are initialized
Initialization runs a class's static field initializers and static blocks. It normally begins when code first:
- Creates an instance with
new. - Reads or writes a non-constant static field.
- Invokes a static method.
- Uses reflection to request initialization.
The JVM initializes the superclass first. Within one class, static initialization follows source order. A compile-time constant, such as static final int SIZE = 10, can be inlined and may not trigger initialization.
Writing a custom class loader
A custom loader usually overrides findClass, not loadClass. This preserves parent delegation: the parent gets the first chance to provide the class.
public final class ByteArrayLoader extends ClassLoader {
private final String name;
private final byte[] bytes;
public ByteArrayLoader(ClassLoader parent, String name, byte[] bytes) {
super(parent);
this.name = name;
this.bytes = bytes;
}
@Override
protected Class<?> findClass(String requested)
throws ClassNotFoundException {
if (!requested.equals(name)) {
throw new ClassNotFoundException(requested);
}
return defineClass(requested, bytes, 0, bytes.length);
}
}Class identity and loading errors
A class's identity is its binary name plus its defining class loader. Two loaders can define separate classes with the same name. Objects from those classes are incompatible, which can cause ClassCastException.
ClassNotFoundExceptionmeans a requested class could not be found.NoClassDefFoundErroroften means a class needed during execution is now unavailable or failed to initialize.
Interpreter and JIT compilation
The interpreter executes bytecode instruction by instruction. The just-in-time (JIT) compiler identifies frequently executed methods or loops and compiles them into native machine code. JIT code can be faster, but compilation takes time and depends on runtime profiling.
Exercise
Why should a custom class loader normally override findClass instead of loadClass?
Show answer
findClass preserves parent delegation, allowing parent and platform loaders to provide classes first.