Understanding Java Class Loading and the Delegation Model
This article explains how Java class loading works, describing the hierarchical delegation model, the three loading phases (loading, linking, initialization), common ClassLoader‑related exceptions, and practical tips for troubleshooting class‑loading issues.
Class Loader Delegation Model
Java classes are loaded by ClassLoaders (CL) which form a tree hierarchy. When a class is requested, the CL first checks its cache; if not found, it delegates the request to its parent CL. If the parent cannot load the class, the current CL attempts to load it from its own search path. The order of lookup is cache → parent CL → current CL, ensuring that the class closest to the root is loaded first and that parent CLs have priority.
The root loader is the bootstrap class loader, implemented in native code and not instantiable from Java. Above it is the extension class loader, which loads classes from the JRE's extension directories, and finally the system/application class loader loads classes from the classpath.
Class Loading Process
Class loading consists of three stages: Loading (physical), Linking, and Initialization .
1. Loading : The JVM searches the configured paths for the class file (e.g., .class, JAR, JSP). If found, the bytecode is read and a basic in‑memory representation of the class is created.
2. Linking is divided into three sub‑steps:
Verification – the bytecode is checked for correctness.
Preparation – memory is allocated for static fields and default values are assigned.
Resolution – symbolic references to super‑classes, interfaces, field types, method signatures, and local variable types are resolved.
3. Initialization : All static initializers and static field assignments are executed. Class loading can be lazy; some parts may be completed earlier if the JVM needs them.
Exceptions
Class‑loading problems often surface later during class usage. Common exceptions include:
ClassNotFoundException – the requested class is not found in the current CL or any of its parents, possibly due to missing archives, incorrect parent settings, or using the wrong CL.
NoClassDefFoundError – similar to the above, but occurs when a previously successfully loaded class cannot be found at runtime, often because of broken symbolic links or parent‑CL misconfiguration.
Practical Tips
1. Where does physical loading occur? It can be from a .class file on disk, a JAR, a JSP, etc., as long as the loader can locate it.
2. When does class loading start? Loading begins when any of the following actions occur: using new , accessing or setting a static field (except final constants), invoking a static method, reflection, initializing a subclass whose superclass is not yet initialized, or JVM startup for the class containing main .
3. Why is the first execution slower? The first time a class is used, the JVM must perform loading, linking, and initialization, which involves I/O and verification overhead. Repeating the operation many times and averaging the time gives a more accurate performance measurement.
4. Extension class loader path – On systems with multiple JREs, JARs placed in /usr/java/packages/lib/ext (Linux) or %SystemRoot%\Sun\Java\lib\ext (Windows) are visible to all JREs.
Author: Zhou Tong, Core Engineer, Hotel Business Unit, Qunar.com, graduate of Harbin Institute of Technology.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.