Sub topic 2
Class loading, verification and linking
This chapter assumes you can compile and run a class or JAR from the command line and know what the classpath is. It explains the work the JVM does for every class before your code can use it. That work is the part of startup the AOT cache later removes.
What happens to a class
The JVM does not load your whole program at launch. It loads each class the first time something needs it. Each class goes through these phases:
- Loading. A class loader finds the
.classbytes, from the JDK runtime image, a JAR or a directory. It parses them into an internal representation. - Linking. Linking has three sub-steps:
- Verification checks that the bytecode is well formed and type-safe.
- Preparation allocates static fields and gives them default values.
- Resolution turns symbolic references, such as a method name in the constant pool, into direct references.
- Initialization. The JVM runs static initializers and assigns static field values.
Initialization is the one phase that runs your own code. The others are JVM bookkeeping, and they repeat identically on every launch.
Class loaders decide where bytes come from
HotSpot uses three built-in loaders, each with its own search area.
| Loader | Loads | Example |
|---|---|---|
| Boot | Core JDK modules | `java.lang.String` |
| Platform | Other JDK modules | `java.sql.Connection` |
| Application | The classpath and module path | `Hello` |
A loader first asks its parent to load a class. It tries itself only if the parent cannot. This delegation means a core class always comes from the boot loader, even if your JAR contains a class with the same name.
Seeing lazy loading and initialization
This program shows that a class is not initialized until it is used.
public class Lazy {
static class Holder {
static {
System.out.println("Holder initialized");
}
static void touch() {
System.out.println("Holder used");
}
}
public static void main(String[] args) {
System.out.println("main started");
Holder.touch();
}
}Compile and run it:
javac Lazy.java
java LazyThe output is:
main started
Holder initialized
Holder usedHolder is loaded and initialized only when touch() is called. The static block runs before the method body.
Counting the work
The class+load log shows every class the JVM loads. Count the lines for a trivial program:
java -Xlog:class+load -cp app.jar Hello | wc -lEven Hello from this tutorial loads several hundred classes. Most come from the JDK itself. The streams, lambdas and method references in Hello pull in many more than a plain println would.
Each log line also names a source. JDK classes may report source: jrt:/java.base or source: shared objects file. Your own classes report a file path or a JAR URL. Later chapters use this field to confirm that the cache is in use.
Why verification is costly
The bytecode verifier proves that a class cannot corrupt the JVM. It checks, for example, that the operand stack never underflows and that every method call matches a declared signature. For a large method this means analyzing control flow and type state at each branch.
By default HotSpot skips verification for classes from the boot loader, because the JDK is trusted. It verifies classes from the application and platform loaders. A framework-heavy application can therefore verify thousands of classes on every start, and the results are the same each time.
Where the time goes
For a typical application, startup spends its time on:
- Reading and parsing class files, including opening JARs and decompressing entries.
- Verifying bytecode.
- Resolving constant-pool entries and building internal structures.
- Running static initializers.
The first three depend only on the class files, not on the input or the machine's state. The JVM repeats them identically on every launch, so they can be done once and reused. Static initializers are different. They may read system properties or the clock, and some have side effects, so the JVM cannot always skip them.
Exercise
Exercise
In the Lazy example, why does "Holder initialized" print after "main started" even though Holder is mentioned in main's code? Which phases must complete for Holder before its static block runs?
Show answer
The JVM loads and initializes a class lazily, at its first active use. That use is the call to Holder.touch(), which happens after the first println. Before the static block runs, Holder must be loaded and then linked (verified, prepared and resolved). Initialization comes last.