Demystifying Java Class Loading: From Load to Initialization
This article explains how the Java Virtual Machine loads .class files into memory, detailing the three-stage process of loading, linking (verification, preparation, resolution), and initialization, while also covering array loading, custom class loaders, and common pitfalls such as deprecated APIs and verification options.
Overview
Any program must load bytecode into memory before the CPU can execute it. The JVM's ClassLoader loads .class files using the parent‑delegation model.
Class Loading Process
Loading consists of three steps: Load, Link, Initialize. The article breaks them down as follows.
Load
The ClassLoader reads the binary byte stream of a class (usually from the classpath) and creates a java.lang.Class object. It performs basic checks such as the magic number, constant‑pool size, and superclass existence.
Link
Linking includes Verification, Preparation, and Resolution.
Verification : ensures the bytecode conforms to the class‑file format and JVM constraints. It can be disabled with -Xverify:none for performance.
Preparation : allocates memory for static fields, assigns default values, and resolves symbolic references to direct references.
Resolution : replaces symbolic references in the constant pool with actual memory addresses.
Initialize
Executes static initializers and <clinit> code, recursively initializing super‑classes. If a class is loaded on demand, initialization occurs when it is first actively used.
Class vs. Array Loading
Arrays have a special type (e.g., Ljava.lang.String) and are created directly by the JVM; their element type is then loaded by the ClassLoader. Ordinary classes can be loaded by the bootstrap loader or custom loaders that override loadClass().
String[] str = new String[10];Common Pitfalls and Tips
In JDK 9, Class.newInstance() is deprecated; use getDeclaredConstructor().newInstance() instead.
Access to private members can be forced with setAccessible(true); otherwise IllegalAccessException is thrown.
Verification is costly but optional for already‑trusted code; it can be skipped with -Xverify:none.
References
《码到成功》
《深入理解Java虚拟机》第三版
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
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.
