Junior — Middle
How does the class loading process occur in JVM during Java program execution?
sobes.tech AI
Answer from AI
In the JVM, the class loading process occurs through a special component — the ClassLoader. When a Java program is launched, the loader searches for the class bytecode (.class file) in specific locations (for example, in the filesystem, JAR archives, or over the network).
The main stages of class loading:
- Loading — the loader finds and reads the class bytecode.
- Linking, which includes:
- Verification of the bytecode correctness.
- Preparation: allocating memory for static fields and initializing them with default values.
- Resolution: linking references to other classes.
- Initialization — executing static blocks and initializing static variables.
The JVM uses a hierarchy of loaders: Bootstrap ClassLoader (loads system classes), Extension ClassLoader, and Application ClassLoader (loads application classes). Loading occurs on demand, when the class is first used.