Anatomy of a Java Class File: Byte-Level Breakdown with HelloWorld
A Java class file is a binary stream of bytes following a strict layout defined by the JVM specification. Each section of the file carries precise class metadata. This analysis walks through the structure using a compiled HelloWorld.class file as a concrete example.
Overall Structure
The top-level structure of a .class file is as follows:
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
Each u1, u2, u4 represents an unsigned integer of 1, 2, or 4 bytes respectively.
Example Source Code
// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
The raw byte dump (hexadecimal) from the compiled class file is shown below. The leftmost column is the byte offset in octal.
0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09
0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07
0000040 00 1c 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29
0000060 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e
0000100 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63
0000120 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01
0000140 00 04 74 68 69 73 01 00 1d 4c 63 6e 2f 69 74 63
0000160 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c 6f
0000200 57 6f 72 6c 64 3b 01 00 04 6d 61 69 6e 01 00 16
0000220 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72
0000240 69 6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13
0000260 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69
0000300 6e 67 3b 01 00 10 4d 65 74 68 6f 64 50 61 72 61
0000320 6d 65 74 65 72 73 01 00 0a 53 6f 75 72 63 65 46
0000340 69 6c 65 01 00 0f 48 65 6c 6c 6f 57 6f 72 6c 64
0000360 2e 6a 61 76 61 0c 00 07 00 08 07 00 1d 0c 00 1e
0000400 00 1f 01 00 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64
0000420 07 00 20 0c 00 21 00 22 01 00 1b 63 6e 2f 69 74
0000440 63 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c
0000460 6f 57 6f 72 6c 64 01 00 10 6a 61 76 61 2f 6c 61
0000500 6e 67 2f 4f 62 6a 65 63 74 01 00 10 6a 61 76 61
0000520 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01 00 03 6f
0000540 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72
0000560 69 6e 74 53 74 72 65 61 6d 3b 01 00 13 6a 61 76
0000600 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d
0000620 01 00 07 70 72 69 6e 74 6c 6e 01 00 15 28 4c 6a
0000640 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b
0000660 29 56 00 21 00 05 00 06 00 00 00 00 00 02 00 01
0000700 00 07 00 08 00 01 00 09 00 00 00 2f 00 01 00 01
0000720 00 00 00 05 2a b7 00 01 b1 00 00 00 02 00 0a 00
0000740 00 00 06 00 01 00 00 00 04 00 0b 00 00 00 0c 00
0000760 01 00 00 00 05 00 0c 00 0d 00 00 00 09 00 0e 00
0001000 0f 00 02 00 09 00 00 00 37 00 02 00 01 00 00 00
0001020 09 b2 00 02 12 03 b6 00 04 b1 00 00 00 02 00 0a
0001040 00 00 00 0a 00 02 00 00 00 06 00 08 00 07 00 0b
0001060 00 00 00 0c 00 01 00 00 00 09 00 10 00 11 00 00
0001100 00 12 00 00 00 05 01 00 10 00 00 00 01 00 13 00
0001120 00 00 02 00 14
Magic Number (bytes 0–3)
The first 4 bytes are always 0xCAFEBABE. This unique signature identifies the file as a valid Java class file.
| Offset | Bytes (hex) |
|---|---|
| 000000 | ca fe ba be |
Version Information (bytes 4–7)
Bytes 4–5 contain the minor version, and bytes 6–7 contain the major version. For example, 00 00 00 34 means minor=0, major=52, which corresponds to Java 8 (Java 7 is 51, Java 9 is 53).
| Offset | Bytes (hex) | Value |
|---|---|---|
| 0000004 | 00 00 00 34 |
major=52, minor=0 → Java 8 |
Constant Pool (bytes 8 onward)
Constant Pool Count (bytes 8–9)
A u2 value that equals the number of entries in the constant pool plus one. 00 23 hex = 35 decimal, so there are 34 entries (indexed 1 through 34). Index 0 is reserved and never used.
Constant Pool Entries
Each entry starts with a 1-byte tag indicating its type. The following table maps tag values to entry types.
| Tag Value | Type |
|---|---|
| 1 | CONSTANT_Utf8 |
| 3 | CONSTANT_Integer |
| 4 | CONSTANT_Float |
| 5 | CONSTANT_Long |
| 6 | CONSTANT_Double |
| 7 | CONSTANT_Class |
| 8 | CONSTANT_String |
| 9 | CONSTANT_Fieldref |
| 10 | CONSTANT_Methodref |
| 11 | CONSTANT_InterfaceMethodref |
| 12 | CONSTANT_NameAndType |
| 15 | CONSTANT_MethodHandle |
| 16 | CONSTANT_MethodType |
| 18 | CONSTANT_InvokeDynamic |
Example: At offset 0000008, the bytes 0a 00 06 00 15 represent a CONSTANT_Methodref (tag 10). The next two u2 values point to constant pool entries #6 (class index) and #21 (name-and-type descriptor). This entry records the constructor method reference.
Byte Dump of Constant Pool (partial)
0000008 0a 00 06 00 15 09 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b ...
This section contains nearly all the strings (CONSTANT_Utf8 entries) needed by the class: method names, descriptors, field names, class names, and source file name.
Access Flags and Inheritance (after constant pool)
After the constant pool, the next u2 values are:
| Byte Offset (hex) | Field | Value | Interpretation |
|---|---|---|---|
| 0000662 | access_flags | 00 21 |
ACC_PUBLIC (0x0001) + ACC_SUPER (0x0020) → public class |
| 0000664 | this_class | 00 05 |
Constant pool index #5 → class name |
| 0000666 | super_class | 00 06 |
Constant pool index #6 → parent class (java/lang/Object) |
| 0000668 | interfaces_count | 00 00 |
No interfaces |
The this_class and super_class indices point to CONSTANT_Class entries in the constant pool.
Fields
| Byte Offset (hex) | Field | Value |
|---|---|---|
| 000066a | fields_count | 00 00 |
Since HelloWorld has no member variables, the fields array is empty.
Methods
| Byte Offset (hex) | Field | Value |
|---|---|---|
| 000066c | methods_count | 00 02 |
Each method is described by a method_info structure:
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
An attribute_info has:
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
Constructor (Method 1)
From offset 0000670:
00 01→ ACC_PUBLIC00 07→ name index #7 →"<init>"00 08→ descriptor index #8 →"()V"00 01→ one attribute (Code)
Code attribute:
00 09→ attribute name"Code"00 00 00 2f→ length 47 bytes00 01 00 01→ max_stack=1, max_locals=12a b7 00 01 b1→ bytecode instructions (aload_0, invokespecial #1, return)00 02→ two sub-attributes: LineNumberTable and LocalVariableTable
LineNumberTable sub-attribute:
00 0a→"LineNumberTable"00 00 00 06→ length 600 01→ one entry00 00 00 04→ bytecode offset 0 maps to source line 4
LocalVariableTable sub-attribute:
00 0b→"LocalVariableTable"00 00 00 0c→ length 1200 01→ one entry00 00 00 05 00 0c→ start_pc=0, length=5, name_index=#12 ("this")00 0d→ descriptor_index=#13 →"Lcn/itcast/jvm/t5/HelloWorld;"00 00→ slot 0
main Method (Method 2)
From offset 0000760 (approximately):
00 09→ ACC_PUBLIC + ACC_STATIC00 0e→ name index #14 →"main"00 0f→ descriptor index #15 →"([Ljava/lang/String;)V"00 02→ two attributes: Code and MethodParameters
Code attribute:
00 09→"Code"00 00 00 37→ length 55 bytes00 02 00 01→ max_stack=2, max_locals=100 00 00 05→ bytecode length 5b2 00 02 12 03 b6 00 04 b1→ instructions: getstatic #2, ldc #3, invokevirtual #4, return00 02→ two sub-attributes: LineNumberTable and LocalVariableTable
LineNumberTable (main):
00 0a→"LineNumberTable"00 00 00 0a→ length 1000 02→ two entries: (0→6), (8→7)
LocalVariableTable (main):
00 0b→"LocalVariableTable"00 00 00 0c→ length 1200 01→ one entry: start_pc=0, length=9, name_index=#16 ("args"), descriptor_index=#17 ("[Ljava/lang/String;"), slot=0
MethodParameters attirbute (second attribute):
00 12→"MethodParameters"00 00 00 05→ length 501→ one parameter00 10→ name_index=#16 ("args")00 00→ access flags (0)
Class-Level Attributes
After all methods, the class may have its own attributes.
| Byte Offset (hex) | Field | Value |
|---|---|---|
| 000111e | attributes_count | 00 01 |
| 0001120 | attribute_name_index | 00 13 → #19 ("SourceFile") |
| 0001122 | attribute_length | 00 00 00 02 |
| 0001126 | info | 00 14 → #20 ("HelloWorld.java") |
This attribute records the source file name, which is useful for debugging.