Fundamentals 9 min read

How Does Java’s JVM Load Classes and Execute Code? A Deep Dive

This article explains how Java classes are loaded into the JVM, details the class loader’s parent‑delegation mechanism, and walks through the execution flow of a sample program, covering stack frames, heap allocation, dynamic linking, method area, program counter, native method stack, and garbage collection.

Lobster Programming
Lobster Programming
Lobster Programming
How Does Java’s JVM Load Classes and Execute Code? A Deep Dive

All Java classes must be loaded into the JVM before they can run. The JVM’s class loader loads compiled .class files, and because .class files can only run on the virtual machine, the JVM interprets them for the operating system.

Example code:

<code>public class JVMTest {
    public static void main(String[] args) {
        JVMTest jvmTest = new JVMTest();
        //调用内部方法
        int result = jvmTest.calculate();
        System.out.println(result + "---------------jvmTest end --------------------");

        //开启一个子线程
        new Thread(() -> System.out.println("我是子线程")).start();
    }

    private int calculate() {
        int v1 = 1;
        int v2 = 5;
        int v3 = (v1 + v2) * 5;
        return v3;
    }
}
</code>

The following diagram shows the coordination of JVM components during execution:

1. Class Loading

After compilation, JVMTest.java becomes JVMTest.class . The class file is loaded by the JVM’s class‑loading subsystem using the parent‑delegation model, which ensures that the bootstrap class loader attempts loading first, followed by child loaders if necessary, preventing duplicate loading and conflicts.

Breaking the parent‑delegation mechanism (e.g., for com.mysql.jdbc.Driver ) can be done by implementing a custom class loader that overrides loadClass() .

2. Execution Process in the JVM

The runtime data area consists of shared regions (heap and method area) and thread‑private regions (stack, native method stack, program counter).

2.1 Stack

When main runs, a stack frame is created for it; calling calculate creates another frame. Each frame contains a local variable table, operand stack, dynamic linking information, and a method exit point.

The operand stack and local variable table work together: values are pushed onto the operand stack, then stored in the local variable table, as illustrated by the execution of v1 = 1 , v2 = 5 , and (v1 + v2) * 5 .

2.2 Heap

The heap is shared by all threads and divided into young and old generations. New objects are allocated in the Eden space; when Eden fills, a minor GC moves surviving objects to survivor spaces (S0, S1). After up to 15 survivor cycles, objects are promoted to the old generation. Large objects may be allocated directly in the old generation, and a full GC is triggered when needed.

2.3 Program Counter

Each thread has a private program counter that records the next bytecode instruction to execute, allowing the thread to resume after a time‑slice.

2.4 Native Method Stack

The native method stack stores frames for native methods (declared with native ), which are implemented in C/C++ libraries such as the thread start method.

2.5 Method Area

The method area, shared among threads, holds the constant pool, static variables, and class metadata. In JDK 8 and later, it uses off‑heap memory. Classes loaded here are executed by the bytecode engine, and static object references point to heap objects.

Summary

Source code is compiled to .class files, which the class loader loads into the JVM.

The JVM runtime data area includes stack, heap, method area, program counter, and native method stack, which cooperate to execute code.

The bytecode execution engine updates the program counter and runs garbage‑collection threads as needed.

JavaJVMGarbage CollectionClass LoadingRuntime Data Area
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.