A class file consists of a single ClassFile structure:
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];
}
The items in the ClassFile structure are as follows:
-
magic
The
magicitem supplies the magic number identifying theclassfile format; it has the value0xCAFEBABE. -
minor_version, major_version
The values of the
minor_versionandmajor_versionitems are the minor and major version numbers of thisclassfile. Together, a major and a minor version number determine the version of theclassfile format. If aclassfile has major version numberMand minor version numberm, we denote the version of itsclassfile format asM.m.A Java Virtual Machine implementation which conforms to Java SE
Nmust support exactly the major versions of theclassfile format specified in the fourth column of Table 4.1-A, "Supported majors". The notation A .. B means major versions A through B, inclusive of both A and B. The third column, "Major", shows the major version introduced by each Java SE release, that is, the first release that could have accepted aclassfile containing thatmajor_versionitem. For very early releases, the JDK version is shown instead of the Java SE release.Table 4.1-A.
classfile format major versionsJava SE Released Major Supported majors 1.0.2 May 1996 45 45 1.1 February 1997 45 45 1.2 December 1998 46 45 .. 46 1.3 May 2000 47 45 .. 47 1.4 February 2002 48 45 .. 48 5.0 September 2004 49 45 .. 49 6 December 2006 50 45 .. 50 7 July 2011 51 45 .. 51 8 March 2014 52 45 .. 52 9 September 2017 53 45 .. 53 10 March 2018 54 45 .. 54 11 September 2018 55 45 .. 55 12 March 2019 56 45 .. 56 13 September 2019 57 45 .. 57 14 March 2020 58 45 .. 58 15 September 2020 59 45 .. 59 16 March 2021 60 45 .. 60 17 September 2021 61 45 .. 61 18 March 2022 62 45 .. 62 19 September 2022 63 45 .. 63 20 March 2023 64 45 .. 64 For a
classfile whosemajor_versionis 56 or above, theminor_versionmust be 0 or 65535.For a
classfile whosemajor_versionis between 45 and 55 inclusive, theminor_versionmay be any value.A historical perspective is warranted on JDK support for
classfile format versions. JDK 1.0.2 supported versions 45.0 through 45.3 inclusive. JDK 1.1 supported versions 45.0 through 45.65535 inclusive. When JDK 1.2 introduced support for major version 46, the only minor version supported under that major version was 0. Later JDKs continued the practice of introducing support for a new major version (47, 48, etc) but supporting only a minor version of 0 under the new major version. Finally, the introduction of preview features in Java SE 12 (see below) motivated a standard role for the minor version of theclassfile format, so JDK 12 supported minor versions of 0 and 65535 under major version 56. Subsequent JDKs introduce support forN.0 andN.65535 whereNis the corresponding major version of the implemented Java SE Platform. For example, JDK 13 supports 57.0 and 57.65535.The Java SE Platform may define preview features. A Java Virtual Machine implementation which conforms to Java SE
N(N≥ 12) must support all the preview features of Java SEN, and none of the preview features of any other Java SE release. The implementation must by default disable the supported preview features, and must provide a way to enable all of them, and must not provide a way to enable only some of them.A
classfile is said to depend on the preview features of Java SEN(N≥ 12) if it has amajor_versionthat corresponds to Java SEN(according to Table 4.1-A) and aminor_versionof 65535.A Java Virtual Machine implementation which conforms to Java SE
N(N≥ 12) must behave as follows:- A
classfile that depends on the preview features of Java SENmay be loaded only when the preview features of Java SENare enabled. - A
classfile that depends on the preview features of another Java SE release must never be loaded. - A
classfile that does not depend on the preview features of any Java SE release may be loaded regardless of whether the preview features of Java SENare enabled.
- A
-
constant_pool_count
The value of the
constant_pool_countitem is equal to the number of entries in theconstant_pooltable plus one. Aconstant_poolindex is considered valid if it is greater than zero and less thanconstant_pool_count, with the exception for constants of typelonganddoublenoted in §4.4.5. -
constant_pool[]
The
constant_poolis a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within theClassFilestructure and its substructures. The format of eachconstant_pooltable entry is indicated by its first "tag" byte.The
constant_pooltable is indexed from 1 toconstant_pool_count- 1. -
access_flags
The value of the
access_flagsitem is a mask of flags used to denote access permissions to and properties of this class or interface. The interpretation of each flag, when set, is specified in Table 4.1-B.Table 4.1-B. Class access and property modifiers
Flag Name Value Interpretation ACC_PUBLIC0x0001 Declared public; may be accessed from outside its package.ACC_FINAL0x0010 Declared final; no subclasses allowed.ACC_SUPER0x0020 Treat superclass methods specially when invoked by the invokespecial instruction. ACC_INTERFACE0x0200 Is an interface, not a class. ACC_ABSTRACT0x0400 Declared abstract; must not be instantiated.ACC_SYNTHETIC0x1000 Declared synthetic; not present in the source code. ACC_ANNOTATION0x2000 Declared as an annotation interface. ACC_ENUM0x4000 Declared as an enumclass.ACC_MODULE0x8000 Is a module, not a class or interface. The
ACC_MODULEflag indicates that thisclassfile defines a module, not a class or interface. If theACC_MODULEflag is set, then special rules apply to theclassfile which are given at the end of this section. If theACC_MODULEflag is not set, then the rules immediately below the current paragraph apply to theclassfile.An interface is distinguished by the
ACC_INTERFACEflag being set. If theACC_INTERFACEflag is not set, thisclassfile defines a class, not an interface or module.If the
ACC_INTERFACEflag is set, theACC_ABSTRACTflag must also be set, and theACC_FINAL,ACC_SUPER,ACC_ENUM, andACC_MODULEflags set must not be set.If the
ACC_INTERFACEflag is not set, any of the other flags in Table 4.1-B may be set exceptACC_ANNOTATIONandACC_MODULE. However, such aclassfile must not have both itsACC_FINALandACC_ABSTRACTflags set (JLS §8.1.1.2).The
ACC_SUPERflag indicates which of two alternative semantics is to be expressed by the invokespecial instruction (§invokespecial) if it appears in this class or interface. Compilers to the instruction set of the Java Virtual Machine should set theACC_SUPERflag. In Java SE 8 and above, the Java Virtual Machine considers theACC_SUPERflag to be set in everyclassfile, regardless of the actual value of the flag in theclassfile and the version of theclassfile.The
ACC_SUPERflag exists for backward compatibility with code compiled by older compilers for the Java programming language. Prior to JDK 1.0.2, the compiler generatedaccess_flagsin which the flag now representingACC_SUPERhad no assigned meaning, and Oracle's Java Virtual Machine implementation ignored the flag if it was set.The
ACC_SYNTHETICflag indicates that this class or interface was generated by a compiler and does not appear in source code.An annotation interface (JLS §9.6) must have its
ACC_ANNOTATIONflag set. If theACC_ANNOTATIONflag is set, theACC_INTERFACEflag must also be set.The
ACC_ENUMflag indicates that this class or its superclass is declared as an enum class (JLS §8.9).All bits of the
access_flagsitem not assigned in Table 4.1-B are reserved for future use. They should be set to zero in generatedclassfiles and should be ignored by Java Virtual Machine implementations. -
this_class
The value of the
this_classitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_infostructure (§4.4.1) representing the class or interface defined by thisclassfile. -
super_class
For a class, the value of the
super_classitem either must be zero or must be a valid index into theconstant_pooltable. If the value of thesuper_classitem is nonzero, theconstant_poolentry at that index must be aCONSTANT_Class_infostructure representing the direct superclass of the class defined by thisclassfile. Neither the direct superclass nor any of its superclasses may have theACC_FINALflag set in theaccess_flagsitem of itsClassFilestructure.If the value of the
super_classitem is zero, then thisclassfile must represent the classObject, the only class or interface without a direct superclass.For an interface, the value of the
super_classitem must always be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_infostructure representing the classObject. -
interfaces_count
The value of the
interfaces_countitem gives the number of direct superinterfaces of this class or interface type. -
interfaces[]
Each value in the
interfacesarray must be a valid index into theconstant_pooltable. Theconstant_poolentry at each value ofinterfaces[i], where 0 ≤ i <interfaces_count, must be aCONSTANT_Class_infostructure representing an interface that is a direct superinterface of this class or interface type, in the left-to-right order given in the source for the type. -
fields_count
The value of the
fields_countitem gives the number offield_infostructures in thefieldstable. Thefield_infostructures represent all fields, both class variables and instance variables, declared by this class or interface type. -
fields[]
Each value in the
fieldstable must be afield_infostructure (§4.5) giving a complete description of a field in this class or interface. Thefieldstable includes only those fields that are declared by this class or interface. It does not include items representing fields that are inherited from superclasses or superinterfaces. -
methods_count
The value of the
methods_countitem gives the number ofmethod_infostructures in themethodstable. -
methods[]
Each value in the
methodstable must be amethod_infostructure (§4.6) giving a complete description of a method in this class or interface. If neither of theACC_NATIVEandACC_ABSTRACTflags are set in theaccess_flagsitem of amethod_infostructure, the Java Virtual Machine instructions implementing the method are also supplied.The
method_infostructures represent all methods declared by this class or interface type, including instance methods, class methods, instance initialization methods (§2.9.1), and any class or interface initialization method (§2.9.2). Themethodstable does not include items representing methods that are inherited from superclasses or superinterfaces. -
attributes_count
The value of the
attributes_countitem gives the number of attributes in theattributestable of this class. -
attributes[]
Each value of the
attributestable must be anattribute_infostructure (§4.7).The attributes defined by this specification as appearing in the
attributestable of aClassFilestructure are listed in Table 4.7-C.The rules concerning attributes defined to appear in the
attributestable of aClassFilestructure are given in §4.7.The rules concerning non-predefined attributes in the
attributestable of aClassFilestructure are given in §4.7.1.
If the ACC_MODULE flag is set in the access_flags item, then no other flag in the access_flags item may be set, and the following rules apply to the rest of the ClassFile structure:
major_version,minor_version: ≥ 53.0 (i.e., Java SE 9 and above)this_class:module-infosuper_class,interfaces_count,fields_count,methods_count: zeroattributes: OneModuleattribute must be present. Except forModule,ModulePackages,ModuleMainClass,InnerClasses,SourceFile,SourceDebugExtension,RuntimeVisibleAnnotations, andRuntimeInvisibleAnnotations, none of the pre-defined attributes (§4.7) may appear.