The class loading process in Java generally consists of three major phases:
- Loading:
- The JVM reads the binary data for a class (commonly from a
.class
file) and creates an in-memory representation of the class.
- The JVM reads the binary data for a class (commonly from a
- Linking:
This phase integrates the class into the running application and is subdivided into three parts:- Verification:
The JVM checks the class’s bytecode to ensure it adheres to the Java language specifications and doesn’t compromise security. - Preparation:
Memory is allocated for the class’s static variables, and these variables are set to their default values. - Resolution:
The JVM replaces the class’s symbolic references (names and descriptors) with direct references (actual memory addresses or pointers), ensuring that the class’s dependencies are properly linked.
- Verification:
- Initialization:
- The class’s static initializers and static blocks are executed. This phase assigns explicit initial values to the static variables and performs any other necessary setup.
These phases work together to ensure that classes are correctly loaded, linked, and initialized before they are used during execution.