Java class file


A Java class file is a file containing Java bytecode that can be executed on the Java Virtual Machine. A Java class file is usually produced by a Java compiler from Java programming language source files containing Java classes. If a source file has more than one class, each class is compiled into a separate class file. Thus, it is called a file because it contains the bytecode for a single class.
JVMs are available for many platforms, and a class file compiled on one platform will execute on a JVM of another platform. This makes Java applications platform-independent.

History

On 11 December 2006, the class file format was modified under Java Specification Request 202.

File layout and structure

Sections

There are 10 basic sections to the Java class file structure:
  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • Access Flags: for example whether the class is abstract, static, etc.
  • This Class: The name of the current class
  • Super Class: The name of the super class
  • Interfaces: Any interfaces in the class
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class

    Magic Number

Class files are identified by the following 4 byte header : CA FE BA BE. The history of this magic number was explained by James Gosling referring to a restaurant in Palo Alto:

"We used to go to lunch at a place called St Michael's Alley. According to local legend, in the deep dark past, the Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in grepping for 4 character hex words that fit after "CAFE" I hit on BABE and decided to use it.
At that time, it didn't seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD - it was eventually replaced by RMI."

General layout

Because the class file contains variable-sized items and does not also contain embedded file offsets, it is typically parsed sequentially, from the first byte toward the end. At the lowest level the file format is described in terms of a few fundamental data types:
  • u1: an unsigned 8-bit integer
  • u2: an unsigned 16-bit integer in big-endian byte order
  • u4: an unsigned 32-bit integer in big-endian byte order
  • table: an array of variable-length items of some type. The number of items in the table is identified by a preceding count number, but the size in bytes of the table can only be determined by examining each of its items.
Some of these fundamental types are then re-interpreted as higher-level values, depending on context.
There is no enforcement of word alignment, and so no padding bytes are ever used.
The overall layout of the class file is as shown in the following table.
Byte offsetSizeType or valueDescription
04 bytesu1 =
0xCA hex
magic number used to identify file as conforming to the class file format
14 bytesu1 =
0xFE hex
magic number used to identify file as conforming to the class file format
24 bytesu1 =
0xBA hex
magic number used to identify file as conforming to the class file format
34 bytesu1 =
0xBE hex
magic number used to identify file as conforming to the class file format
42 bytesu2minor version number of the class file format being used
52 bytesu2minor version number of the class file format being used
62 bytesu2major version number of the class file format being used.

Java SE 25 = 69,

Java SE 24 = 68,

Java SE 23 = 67,

Java SE 22 = 66,

Java SE 21 = 65,

Java SE 20 = 64,

Java SE 19 = 63,

Java SE 18 = 62,

Java SE 17 = 61,

Java SE 16 = 60,

Java SE 15 = 59,

Java SE 14 = 58,

Java SE 13 = 57,

Java SE 12 = 56,

Java SE 11 = 55,

Java SE 10 = 54,

Java SE 9 = 53,

Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45.
For details of earlier version numbers see footnote 1 at
72 bytesu2major version number of the class file format being used.

Java SE 25 = 69,

Java SE 24 = 68,

Java SE 23 = 67,

Java SE 22 = 66,

Java SE 21 = 65,

Java SE 20 = 64,

Java SE 19 = 63,

Java SE 18 = 62,

Java SE 17 = 61,

Java SE 16 = 60,

Java SE 15 = 59,

Java SE 14 = 58,

Java SE 13 = 57,

Java SE 12 = 56,

Java SE 11 = 55,

Java SE 10 = 54,

Java SE 9 = 53,

Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45.
For details of earlier version numbers see footnote 1 at
82 bytesu2constant pool count, number of entries in the following constant pool table. This count is at least one greater than the actual number of entries; see following discussion.
92 bytesu2constant pool count, number of entries in the following constant pool table. This count is at least one greater than the actual number of entries; see following discussion.
10cpsize tableconstant pool table, an array of variable-sized constant pool entries, containing items such as literal numbers, strings, and references to classes or methods. Indexed starting at 1, containing number of entries in total.
...cpsize tableconstant pool table, an array of variable-sized constant pool entries, containing items such as literal numbers, strings, and references to classes or methods. Indexed starting at 1, containing number of entries in total.
...cpsize tableconstant pool table, an array of variable-sized constant pool entries, containing items such as literal numbers, strings, and references to classes or methods. Indexed starting at 1, containing number of entries in total.
...cpsize tableconstant pool table, an array of variable-sized constant pool entries, containing items such as literal numbers, strings, and references to classes or methods. Indexed starting at 1, containing number of entries in total.
10+cpsize2 bytesu2access flags, a bitmask
11+cpsize2 bytesu2access flags, a bitmask
12+cpsize2 bytesu2identifies this class, index into the constant pool to a "Class"-type entry
13+cpsize2 bytesu2identifies this class, index into the constant pool to a "Class"-type entry
14+cpsize2 bytesu2identifies super class, index into the constant pool to a "Class"-type entry
15+cpsize2 bytesu2identifies super class, index into the constant pool to a "Class"-type entry
16+cpsize2 bytesu2interface count, number of entries in the following interface table
17+cpsize2 bytesu2interface count, number of entries in the following interface table
18+cpsizeisize tableinterface table: a variable-length array of constant pool indexes describing the interfaces implemented by this class
...isize tableinterface table: a variable-length array of constant pool indexes describing the interfaces implemented by this class
...isize tableinterface table: a variable-length array of constant pool indexes describing the interfaces implemented by this class
...isize tableinterface table: a variable-length array of constant pool indexes describing the interfaces implemented by this class
18+cpsize+''isize2 bytesu2field count, number of entries in the following field table
19+cpsize+isize2 bytesu2field count, number of entries in the following field table
20+cpsize+isizefsize tablefield table, variable length array of fields
each element is a field_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.5
...fsize tablefield table, variable length array of fields
each element is a field_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.5
...fsize tablefield table, variable length array of fields
each element is a field_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.5
...fsize tablefield table, variable length array of fields
each element is a field_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.5
20+cpsize+isize+fsize2 bytesu2method count, number of entries in the following method table
21+cpsize+isize+fsize2 bytesu2method count, number of entries in the following method table
22+cpsize+isize+fsizemsize tablemethod table, variable length array of methods
each element is a method_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6
...msize tablemethod table, variable length array of methods
each element is a method_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6
...msize tablemethod table, variable length array of methods
each element is a method_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6
...msize tablemethod table, variable length array of methods
each element is a method_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6
22+cpsize+isize+fsize+msize2 bytesu2attribute count, number of entries in the following attribute table
23+cpsize+isize+fsize+msize2 bytesu2attribute count, number of entries in the following attribute table
24+cpsize+isize+fsize+msizeasize tableattribute table, variable length array of attributes
each element is an attribute_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7
...asize tableattribute table, variable length array of attributes
each element is an attribute_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7
...asize tableattribute table, variable length array of attributes
each element is an attribute_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7
...asize'' tableattribute table, variable length array of attributes
each element is an attribute_info structure defined in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7