Sub topic 11
Linking: Verification, Preparation and Resolution
This chapter assumes you can compile and run basic Java programs and understand classes, methods, static members, and exceptions. You should also know how to use javac, java, and javap.
Where linking fits
The JVM processes a class in stages:
- Loading reads the class file and creates a
Classobject. - Linking makes the class safe and usable. It includes verification, preparation, and resolution.
- Initialization runs static field assignments and static initializer blocks.
Linking happens before initialization, but the JVM may delay some resolution work until the class needs it.
Verification
Verification checks that the class file follows JVM rules. The verifier checks details such as:
- The file has a valid structure and supported version.
- Bytecode instructions use valid operand types.
- Branches lead to valid instruction locations.
- Methods receive and return the expected types.
- Access rules are not violated.
- Stack frames remain type-safe at every possible execution path.
For example, an instruction that adds two integers cannot receive a reference and an integer. Rejecting that class before execution protects the JVM from malformed or unsafe bytecode.
A failed structural check can produce ClassFormatError. Invalid bytecode often produces VerifyError. These errors usually indicate a damaged class file, an incompatible bytecode generator, or a compiler and runtime mismatch.
Preparation
During preparation, the JVM allocates storage for static fields and assigns default values:
- Numeric fields receive zero.
booleanfields receivefalse.- Reference fields receive
null.
The field's explicit initializer does not run yet. That happens during initialization.
A compile-time constant, such as static final int SIZE = 10, can receive its constant value during preparation. A nonconstant initializer such as static int size = readSize() waits until initialization.
public class PrepareDemo {
static final int LIMIT = 10;
static int count = createCount();
static int createCount() {
System.out.println("initializer runs");
return 3;
}
public static void main(String[] args) {
System.out.println(LIMIT);
System.out.println(count);
}
}The JVM prepares storage before main executes, but createCount() runs only when PrepareDemo is initialized.
Resolution and symbolic references
The constant pool stores symbolic references to classes, fields, and methods. A symbolic reference names a target without storing a direct memory address. Resolution changes such a reference into a usable runtime reference.
For example, bytecode may refer to java/lang/String.length:()I. The JVM must locate the class, find the method, and check that the access is allowed.
Resolution can occur eagerly or lazily. Lazy resolution lets the JVM avoid resolving a method or class that the program never uses.
$ javac MathOps.java
$ javap -c MathOps
public static int add(int, int);
Code:
0: iload_0
1: iload_1
2: iadd
3: ireturnThe method reference and class references needed by this code come from the constant pool. The JVM resolves them as execution requires them.
Common errors
| Error | Typical meaning |
|---|---|
| `VerifyError` | The bytecode fails verification |
| `NoSuchMethodError` | A resolved method is missing or incompatible |
| `IllegalAccessError` | Code violates access rules |
| `UnsupportedClassVersionError` | The runtime cannot read the class-file version |
A ClassNotFoundException usually means an explicit class-loading request failed. A NoClassDefFoundError often appears when a required class was unavailable during normal execution or when earlier loading, linking, or initialization failed.
Exercise
Why does the assignment static int count = createCount() not run during preparation?
Show answer
Preparation allocates static storage and assigns default values. Java code in field initializers runs during class initialization.