Understanding Java Class File Structure and JVM Loading Process
This article explains the internal structure of Java class files—including magic numbers, versioning, constant pools, access flags, fields, methods, and attributes—illustrates bytecode generation with a sample class, and details the JVM's class loading, verification, preparation, resolution, initialization, memory allocation, object layout, and execution engine.
A Java class file, after compilation, consists of a compact binary format that includes a magic number, version information, a constant pool, access flags, references to the current class and its superclass, interface indices, field and method tables, and attribute collections.
The constant pool stores unsigned numbers (u1, u2, u4, u8) and tables; entries such as Methodref, Fieldref, and Class provide symbolic references that are later resolved.
Example class:
package com.jd.crm.Logback;
public class TestClass implements Super {
private static final int staticVar = 0;
private int instanceVar = 0;
public int instanceMethod(int param) throws Exception {
return param++;
}
}
interface Super { }Running javap on the compiled TestClass.class yields detailed information about the class file, including the constant pool entries, method descriptors, bytecode instructions, and attribute tables such as LineNumberTable and LocalVariableTable .
Key sections of the class file:
Magic number: first four bytes cafebabe identify a valid class file.
Version: two-byte minor and major version numbers (e.g., major 52 for JDK 1.8).
Constant pool count and entries: literals and symbolic references.
Access flags: indicate modifiers like ACC_PUBLIC and ACC_SUPER .
This class, super class, and interface indices.
Field table: defines static and instance fields with access flags, name and descriptor indices, and attributes (e.g., ConstantValue ).
Method table: includes constructors and regular methods, each with a Code attribute describing stack size, local variables, bytecode length, and nested attributes.
Attribute table: contains various attributes such as Code , Exceptions , MethodParameters , etc.
The JVM loads a class through several phases:
Loading : the class loader reads the binary stream and creates a Class object in the method area.
Verification : checks file format, metadata, bytecode correctness, and symbolic references.
Preparation : allocates memory for static fields and sets default values (e.g., zero for primitives).
Resolution : resolves symbolic references in the constant pool to direct references.
Initialization : executes static initializers and <clinit> to assign explicit static values.
During object creation, the JVM allocates memory in the heap, initializes it to zero, sets the object header (mark word and class pointer), and then runs the instance initializer ( <init> ) to set instance fields.
Object access can be performed via a handle (indirect reference) or a direct pointer; HotSpot uses the direct pointer approach for performance.
The execution engine is stack‑based: each method call creates a stack frame containing a local variable table, an operand stack, a dynamic link, and a return address. Bytecode instructions manipulate the operand stack and local variables, and method invocation instructions ( invokevirtual , invokespecial , invokestatic , invokeinterface , invokedynamic ) determine how the target method is resolved.
Additional topics covered include the runtime constant pool, string interning, memory allocation strategies (pointer bumping vs. free‑list), thread‑local allocation buffers (TLAB), and synchronization mechanisms.
Understanding these low‑level details helps developers diagnose heap overflows, memory leaks, and performance issues related to Java garbage collection and JVM internals.
JD Tech
Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.
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.