Class loading and linking
What you will learn
Loading creates a runtime type from its binary definition. Linking checks and prepares that representation and resolves symbolic references as needed. Type identity includes the defining loader as well as the binary name.
- Identify built-in loader roles.
- Distinguish loading, verification, preparation and resolution.
- Diagnose missing definitions safely.
Each example is a complete Main.java file. Use a fresh lab-owned directory: javac --release 21 -proc:none Main.java, then java Main. Use only trusted, locally compiled classes and fixed synthetic inputs. The runner enforces a process deadline.
Inspect standard loaders
public class Main {
public static void main(String[] args) throws Exception { ClassLoader app = Main.class.getClassLoader(); System.out.println(String.class.getClassLoader() == null); System.out.println(app == ClassLoader.getSystemClassLoader()); System.out.println(app.getParent() == ClassLoader.getPlatformClassLoader()); }}Expected output:
truetruetrueThese observations describe the default local launcher; custom launchers can change the arrangement.
Loader roles and identity
The bootstrap loader defines core runtime types and is represented by null in Class.getClassLoader. The platform loader handles platform classes not defined by bootstrap; the system loader normally handles the application classpath. The usual ClassLoader implementation checks already loaded types, asks its parent or bootstrap, then tries to find a definition. Custom loaders may use different policies. Two definitions of the same binary name by distinct defining loaders are distinct runtime types.
Linking is not initialization
Verification checks structural and type constraints. Preparation establishes static fields with default values. Constant variables receive their specified values during initialization before ordinary static initialization code. Resolution converts symbolic references into runtime entities; a VM may resolve references lazily or eagerly within specification rules. Loading and linking can therefore happen before the first active use.
Diagnose by phase
ClassNotFoundException commonly indicates a requested name could not be located by a loading API. NoClassDefFoundError is a linkage error that may arise from a missing required definition or a prior initialization failure. NoSuchMethodError can occur if a caller was compiled against an API whose runtime binary no longer provides that method. Inspect actual runtime classpath and versions and rebuild dependent modules together. Do not catch LinkageError as ordinary business recovery.
Load without initialization
public class Main { static class Item { static { System.out.println("initialized"); } } public static void main(String[] args) throws Exception { Class<?> type = Class.forName("Main$Item", false, Main.class.getClassLoader()); System.out.println(type.getName()); System.out.println(type == Item.class); }}Expected output:
Main$ItemtrueThe nested binary name uses a dollar sign. Neither the false flag nor the class literal triggers initialization.
Diagnose a missing name
public class Main {
public static void main(String[] args) throws Exception { try { Class.forName("bytemind.lab.MissingType", false, Main.class.getClassLoader()); } catch (ClassNotFoundException e) { System.out.println("not found: " + e.getMessage()); } }}Expected output:
not found: bytemind.lab.MissingTypeThis fixed nonexistent name fails in the checked loading API; it is not a verifier failure.
Common mistakes
Do not confuse package names with loader identity, parent delegation with an unbreakable rule, or loading with running static initializers. Never load arbitrary user-supplied classes.
Practice
Load the fixed name java.lang.String without initialization using the application loader and print whether it equals String.class and its defining loader is null.
Expected output:
truetrueCompare your answer with the manual practice criteria.
Official reading: Loading. Java 25 execution remains unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Class loading and linking — knowledge check
Original JVM architecture questions.
What defines ordinary class identity?
What does null from String.class.getClassLoader indicate?
What does preparation establish?
What does Class.forName(name, false, loader) avoid?
What can cause NoSuchMethodError?