How Java Loads Classes: Inside the JVM Class Loading Mechanism

1. What Is Class Loading?

Class loading refers to the process by which the Java Virtual Machine (JVM) loads class definitions (from .class files) into memory, verifies, transforms, resolves, and initializes them into runtime structures that the JVM can directly use.

Unlike many other languages, Java performs type loading, linking, and initialization at runtime, enabling dynamic loading and execution.

2. Class Loading Process Overview

The JVM defines five main phases in the class loading process:

  1. Loading
  2. Verification
  3. Preparation
  4. Resolution
  5. Initialization

The first three stages are part of the loading process. The latter two are part of the linking phase.

2.1 Loading

Java 8 Update:
The PermGen space was removed and replaced by Metaspace, which resides in native memory.

Java 15 Update:
The introduction of Hidden Classes allows dynamically generated classes to be used internally without being exposed through a ClassLoader.

2.2 Verification

Ensures the class file is safe and conforms to the JVM specification.

This step guarantees runtime security and prevents JVM crashes due to malformed classes.

2.3 Preparation

Java 8 and beyond: Static variables are stored on the heap along with the Class object rather than in the method area.

2.4 Resolution

Resolution can be eager (at load time) or lazy (when first used) depending on the JVM implementation.

2.5 Initialization

<clinit>() is not always present. If there are no static initializations or static blocks, it won’t be generated.

Thread-Safety

Parent Class Initialization Order

The parent class is always initialized before the child class.

class Parent {
    static { System.out.println("Parent static"); }
}

class Child extends Parent {
    static { System.out.println("Child static"); }
}

public class Test {
    public static void main(String[] args) {
        new Child();
    }
}
//Output:
//Parent static
//Child static