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 + "!");
    }
}
TypeFieldWhat it holds
u4magic0xCAFEBABE
u2minor_version
u2major_versionJava release + 44
u2constant_pool_countone more than the highest index
cp_info[…]constant_poolentries 1 to count − 1
u2access_flagspublic, final, interface, abstract, enum, record…
u2this_classa Class constant
u2super_classa Class constant, or 0 for Object
u2interfaces_count
u2[…]interfacesClass constants
u2fields_count
field_info[…]fieldsflags, name, descriptor, attributes
u2methods_count
method_info[…]methodsflags, name, descriptor, attributes (Code holds the bytecode)
u2attributes_count
attribute_info[…]attributesSourceFile, 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.

OffsetBytesFieldValue
08 bytesheader
0ca fe ba bemagic0xCAFEBABE
400 00minor_version0
600 45major_version69 (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.

OffsetBytesFieldValue
8842 bytesconstant pool57 slots
800 3aconstant_pool_count58
105 bytes#1 Methodrefjava/lang/Object.<init>:()V
100atag10 (Methodref)
1100 02class_index#2
1300 03name_and_type_index#3
153 bytes#2 Classjava/lang/Object
1507tag7 (Class)
1600 04name_index#4
185 bytes#3 NameAndType<init>:()V
180ctag12 (NameAndType)
1900 05name_index#5
2100 06descriptor_index#6
2319 bytes#4 Utf8"java/lang/Object"
2301tag1 (Utf8)
2400 10length16
266a 61 76 61 2f 6c 61 6e 67 2f 4f 62 …bytes"java/lang/Object"
429 bytes#5 Utf8"<init>"
4201tag1 (Utf8)
4300 06length6
453c 69 6e 69 74 3ebytes"<init>"
516 bytes#6 Utf8"()V"
5101tag1 (Utf8)
5200 03length3
5428 29 56bytes"()V"
573 bytes#7 Stringworld
5708tag8 (String)
5800 08string_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.

OffsetBytesFieldValue
8508 bytesclass
85000 21access_flags0x0021 public super
85200 19this_class#25
85400 02super_class#2
8562 bytesinterfaces
85600 00interfaces_count0

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.

OffsetBytesFieldValue
8582 bytesfields
85800 00fields_count0

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.

OffsetBytesFieldValue
860177 bytesmethods
86000 02methods_count2
86261 bytesmethod <init>()V
86200 01access_flags0x0001 public
86400 05name_index#5
86600 06descriptor_index#6
86800 01 00 1b 00 00 00 2f 00 01 00 01 …attributes1
923114 bytesmethod main([Ljava/lang/String;)V
92300 09access_flags0x0009 public static
92500 20name_index#32
92700 21descriptor_index#33
92900 01 00 1b 00 00 00 64 00 02 00 02 …attributes1

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.

OffsetBytesFieldValue
103740 bytesattributes3
103700 03attributes_count3
10398 bytesSourceFile
103900 29attribute_name_index#41
104100 00 00 02attribute_length2
104500 2asourcefile_index#42
104714 bytesBootstrapMethods
104700 2battribute_name_index#43
104900 00 00 08attribute_length8
105300 01num_bootstrap_methods1
105500 2e 00 01 00 2cbootstrap method 0
106116 bytesInnerClasses
106100 34attribute_name_index#52
106300 00 00 0aattribute_length10
106700 01number_of_classes1
106900 35 00 37 00 39 00 19class 0java/lang/invoke/MethodHandles$Lookup

Explore Hello.class in the viewer

Specification: JVMS chapter 4, The class File Format (Java SE 25).