The Plain Picture#
The stage crew doesn't bring every prop in before the show — only when a scene needs it. Each prop is fetched, inspected for safety, set up, and then used. And there's a chain of command: a junior crew member asks their supervisor first, "do we already have this prop from the official store?"
The Real Names#
Classes are loaded lazily — the first time they're actually used — in three phases:
Loading — find the
.classbytes (in a.jarfile, a folder, or the JDK itself) and create the class's internal representation in metaspace (Ch 3).Linking:
verification — check the bytecode is valid and safe (no jumping into the middle of an instruction, no using an
intas a reference);preparation — allocate
staticfields and set them to default values (0, null);resolution — turn symbolic references ("the method
scorein classQuiz") into direct references (often done lazily later).
Initialisation — run the class's
staticinitialisers and static field assignments, once, in order (Object-Oriented Programming Ch 9). It happens on first active use: creating an instance, calling a static method, reading a non-constant static field.
Class loaders form a hierarchy:
Bootstrap class loader core JDK classes (java.lang.String, java.util.*) — built into the JVM
▲
Platform class loader other JDK modules
▲
Application class loader YOUR classes and libraries (the "classpath")
▲
custom loaders frameworks, app servers, pluginsParent delegation: asked to load a class, a loader first asks its parent; only if the parent can't find it does it load the class itself. So the
bootstrap loader always supplies java.lang.String — nobody can sneak in a fake String class by putting one on the classpath.
Under the Hood — two confusingly named errors#
ClassNotFoundException | NoClassDefFoundError | |
|---|---|---|
| Kind | checked exception | error (LinkageError) |
| When | code explicitly asks for a class by name — Class.forName("com.x.Driver"), reflection — and it can't be found | a class was present when compiling but is missing or failed to initialise at run time |
| Typical cause | a missing driver/plugin JAR; a typo in a class name | a dependency JAR missing from the deployment; or a static initialiser that threw (then every later use gives NoClassDefFoundError) |
Check yourself
4 questionsName the three phases of class loading, and the three parts of linking.
Show answerHide answer
Loading, linking (verification, preparation, resolution), initialisation.
When does a class's static initialiser run?
Show answerHide answer
Once, on the class's first active use (instance creation, static method call, non-constant static field access).
What is parent delegation, and why does it matter for security?
Show answerHide answer
A loader asks its parent before loading a class itself, so core classes always come from the trusted bootstrap loader and can't be replaced.
ClassNotFoundException vs NoClassDefFoundError?
Show answerHide answer
ClassNotFoundException: a class requested by name at run time (e.g. Class.forName) isn't found. NoClassDefFoundError: a class available at compile time
is missing or failed to initialise at run time.