Sub topic 2
Inside a `.class` File
Prerequisites
Assume you can compile a Java source file with javac, run a class with java, and recognize classes, methods, fields, and exceptions. This chapter focuses on the binary format that the JVM reads after compilation.
The class file structure
A .class file is a portable binary description of one Java class or interface. It is not machine code for one processor. The JVM reads its instructions and data, verifies them, and later executes them.
The main parts appear in this order:
| Part | Purpose |
|---|---|
| Magic number | Identifies the file as a Java class file |
| Version | Records the class-file format version |
| Constant pool | Stores names, types, strings, and symbolic references |
| Access flags | Describes properties such as `public`, `final`, or `interface` |
| Class references | Identifies this class and its superclass |
| Interfaces, fields, methods | Describes the class members |
| Attributes | Holds extra data, including method bytecode |
The values use a defined binary format. Multibyte values are stored in big-endian order, with the most significant byte first.
Every class file starts with the four-byte magic number 0xCAFEBABE. The next two bytes hold the minor version, and the following two hold the major version. Java 11 uses major version 55.
public class MathOps {
public static int add(int a, int b) {
return a + b;
}
}Compile and inspect the class:
$ javac MathOps.java
$ javap -verbose MathOpsThe -verbose option displays class-file details. A shorter bytecode view is:
$ javap -c MathOps
...
public static int add(int, int);
Code:
0: iload_0
1: iload_1
2: iadd
3: ireturnThe method's bytecode is stored inside its Code attribute. The method entry itself also records its name, descriptor, access flags, and other attributes.
Reading the header
This program reads the first eight bytes:
import java.io.DataInputStream;
import java.io.FileInputStream;
public class ClassHeader {
public static void main(String[] args) throws Exception {
try (DataInputStream in =
new DataInputStream(new FileInputStream("MathOps.class"))) {
int magic = in.readInt();
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
System.out.printf("magic=%08X minor=%d major=%d%n",
magic, minor, major);
}
}
}Run it after compiling MathOps.java:
$ javac ClassHeader.java
$ java ClassHeader
magic=CAFEBABE minor=0 major=55The class file also contains counts before variable-length sections. For example, constant_pool_count says how many constant-pool entries follow. The count is one greater than the highest usable index because constant-pool indexing starts at 1.
The constant pool
The constant pool is a table of reusable values and symbolic references. Entries can represent:
- UTF-8 text such as class, method, and field names
- Integer, floating-point, and string constants
- A class name
- A field or method reference
- A name-and-type pair
A method reference usually points to a class entry and a name-and-type entry. This lets bytecode refer to MathOps.add:(II)I without storing a processor-specific address. The JVM resolves that symbolic reference during linking or execution.
A useful mental model
The JVM uses this structure to load and link a class. The class file supplies descriptions and bytecode; the JVM supplies runtime memory, verification, resolution, and execution.
Exercise
A class compiled by Java 11 reports major version 55. What does that number describe?
Show answer
It describes the class-file format version, not the Java source version written in the file.