The Java class file format
What javac writes and the JVM loads, from the first byte to the last, using a real 1,077-byte class compiled by javac 25.
A class file describes exactly one class, interface, enum, record or module. It is a flat sequence of big-endian fields with no padding and no offsets: to find the methods you have to read everything before them, starting with the constant pool. Here is the whole structure, then each part of Hello.class, compiled from this source:
public class Hello {
public static void main(String[] args) {
String who = args.length > 0 ? args[0] : "world";
System.out.println("Hello, " + who + "!");
}
}| Type | Field | What it holds |
|---|---|---|
u4 | magic | 0xCAFEBABE |
u2 | minor_version | |
u2 | major_version | Java release + 44 |
u2 | constant_pool_count | one more than the highest index |
cp_info[…] | constant_pool | entries 1 to count − 1 |
u2 | access_flags | public, final, interface, abstract, enum, record… |
u2 | this_class | a Class constant |
u2 | super_class | a Class constant, or 0 for Object |
u2 | interfaces_count | |
u2[…] | interfaces | Class constants |
u2 | fields_count | |
field_info[…] | fields | flags, name, descriptor, attributes |
u2 | methods_count | |
method_info[…] | methods | flags, name, descriptor, attributes (Code holds the bytecode) |
u2 | attributes_count | |
attribute_info[…] | attributes | SourceFile, InnerClasses, BootstrapMethods… |
The header 8 bytes, 0.74%
The magic number CAFEBABE, then the version: minor first, then major. Major version 69 is Java 25 (all versions). A JVM refuses a class whose major version is newer than its own.
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 0 | 8 bytes | header | |
| 0 | ca fe ba be | magic | 0xCAFEBABE |
| 4 | 00 00 | minor_version | 0 |
| 6 | 00 45 | major_version | 69 (Java 25) |
The constant pool 842 bytes, 78.2%
Every name, descriptor, string and number the class uses, each stored once and referred to by index from everywhere else. It is 78.2% of this file, which is typical. The first entries are the method that the constructor calls, Object.<init>: a Methodref pointing at a Class and a NameAndType, which point at Utf8 text. All 17 kinds of constant.
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 8 | 842 bytes | constant pool | 57 slots |
| 8 | 00 3a | constant_pool_count | 58 |
| 10 | 5 bytes | #1 Methodref | java/lang/Object.<init>:()V |
| 10 | 0a | tag | 10 (Methodref) |
| 11 | 00 02 | class_index | #2 |
| 13 | 00 03 | name_and_type_index | #3 |
| 15 | 3 bytes | #2 Class | java/lang/Object |
| 15 | 07 | tag | 7 (Class) |
| 16 | 00 04 | name_index | #4 |
| 18 | 5 bytes | #3 NameAndType | <init>:()V |
| 18 | 0c | tag | 12 (NameAndType) |
| 19 | 00 05 | name_index | #5 |
| 21 | 00 06 | descriptor_index | #6 |
| 23 | 19 bytes | #4 Utf8 | "java/lang/Object" |
| 23 | 01 | tag | 1 (Utf8) |
| 24 | 00 10 | length | 16 |
| 26 | 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 … | bytes | "java/lang/Object" |
| 42 | 9 bytes | #5 Utf8 | "<init>" |
| 42 | 01 | tag | 1 (Utf8) |
| 43 | 00 06 | length | 6 |
| 45 | 3c 69 6e 69 74 3e | bytes | "<init>" |
| 51 | 6 bytes | #6 Utf8 | "()V" |
| 51 | 01 | tag | 1 (Utf8) |
| 52 | 00 03 | length | 3 |
| 54 | 28 29 56 | bytes | "()V" |
| 57 | 3 bytes | #7 String | world |
| 57 | 08 | tag | 8 (String) |
| 58 | 00 08 | string_index | #8 |
… and more: open the class in the viewer to see every byte.
Flags, this class and its supertypes 8 bytes, 0.74%
Access flags as a bit mask (0x0021 is public and ACC_SUPER), then indexes of Class constants for this class and its superclass, and the interfaces it implements.
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 850 | 8 bytes | class | |
| 850 | 00 21 | access_flags | 0x0021 public super |
| 852 | 00 19 | this_class | #25 |
| 854 | 00 02 | super_class | #2 |
| 856 | 2 bytes | interfaces | |
| 856 | 00 00 | interfaces_count | 0 |
Fields 2 bytes, 0.19%
Each field has flags, a name and a descriptor (I for int, Ljava/lang/String; for String), and attributes such as ConstantValue or Signature. Hello has none.
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 858 | 2 bytes | fields | |
| 858 | 00 00 | fields_count | 0 |
Methods 177 bytes, 16.4%
Each method has flags, a name, a descriptor and attributes. The bytecode is in its Code attribute, along with the stack and local variable sizes and nested attributes for line numbers, local variable names and stack map frames. The instruction set has a page for every opcode.
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 860 | 177 bytes | methods | |
| 860 | 00 02 | methods_count | 2 |
| 862 | 61 bytes | method <init> | ()V |
| 862 | 00 01 | access_flags | 0x0001 public |
| 864 | 00 05 | name_index | #5 |
| 866 | 00 06 | descriptor_index | #6 |
| 868 | 00 01 00 1b 00 00 00 2f 00 01 00 01 … | attributes | 1 |
| 923 | 114 bytes | method main | ([Ljava/lang/String;)V |
| 923 | 00 09 | access_flags | 0x0009 public static |
| 925 | 00 20 | name_index | #32 |
| 927 | 00 21 | descriptor_index | #33 |
| 929 | 00 01 00 1b 00 00 00 64 00 02 00 02 … | attributes | 1 |
Class attributes 40 bytes, 3.7%
Attributes about the class as a whole: the source file name, inner classes, and the bootstrap methods that its invokedynamic instructions use. Hello's string concatenation is an invokedynamic, so it has a BootstrapMethods attribute. All attributes.
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1037 | 40 bytes | attributes | 3 |
| 1037 | 00 03 | attributes_count | 3 |
| 1039 | 8 bytes | SourceFile | |
| 1039 | 00 29 | attribute_name_index | #41 |
| 1041 | 00 00 00 02 | attribute_length | 2 |
| 1045 | 00 2a | sourcefile_index | #42 |
| 1047 | 14 bytes | BootstrapMethods | |
| 1047 | 00 2b | attribute_name_index | #43 |
| 1049 | 00 00 00 08 | attribute_length | 8 |
| 1053 | 00 01 | num_bootstrap_methods | 1 |
| 1055 | 00 2e 00 01 00 2c | bootstrap method 0 | |
| 1061 | 16 bytes | InnerClasses | |
| 1061 | 00 34 | attribute_name_index | #52 |
| 1063 | 00 00 00 0a | attribute_length | 10 |
| 1067 | 00 01 | number_of_classes | 1 |
| 1069 | 00 35 00 37 00 39 00 19 | class 0 | java/lang/invoke/MethodHandles$Lookup |
Explore Hello.class in the viewer
Specification: JVMS chapter 4, The class File Format (Java SE 25).