Contents Prev Next Up


2.1 Format

The following pseudo-structure gives a top-level description of the format of a class file:

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[attribute_count];
}

magic

This field must have the value 0xCAFEBABE.

minor_version, major_version

These fields contain the version number of the Java compiler that produced this class file. An implementation of the virtual machine will normally support some range of minor version numbers 0-n of a particular major version number. If the minor version number is incremented the new code won't run on the old virtual machines, but it is possible to make a new virtual machine which can run versions up to n+1.

A change of the major version number indicates a major incompatible change, one that requires a different virtual machine that may not support the old major version in any way.

The current major version number is 45; the current minor version number is 3.

constant_pool_count

This field indicates the number of entries in the constant pool in the class file.

constant_pool

The constant pool is an table of values. These values are the various string constants, class names, field names, and others that are referred to by the class structure or by the code.

constant_pool[0] is always unused by the compiler, and may be used by an implementation for any purpose.

Each of the constant_pool entries 1 through constant_pool_count-1 is a variable-length entry, whose format is given by the first ''tag'' byte, as described in section 2.3.

access_flags

This field contains a mask of up to sixteen modifiers used with class, method, and field declarations. The same encoding is used on similar fields in field_info and method_info as described below. Here is the encoding:
 
Flag NameValueMeaningUsed By
ACC_PUBLIC0x0001Visible to everyone Class, Method, Variable
ACC_PRIVATE0x0002Visible only to the defining class Method, Variable
ACC_PROTECTED0x0004Visible to subclasses Method, Variable
ACC_STATIC0x0008Variable or method is static Method, Variable
ACC_FINAL0x0010No further subclassing, overriding, or assignment after initializationClass, Method, Variable
ACC_SYNCHRONIZED0x0020Wrap use in monitor lockMethod
ACC_VOLATILE0x0040Can't cacheVariable
ACC_TRANSIENT0x0080Not to be written or read by a persistent object manager Variable
ACC_NATIVE0x0100Implemented in a language other than JavaMethod
ACC_INTERFACE0x0200Is an interface Class
ACC_ABSTRACT0x0400No body provided Class, Method

this_class

This field is an index into the constant pool; constant_pool[this_class] must be a CONSTANT_class.

super_class

This field is an index into the constant pool. If the value of super_class is nonzero, then constant_pool[super_class] must be a class, and gives the index of this class's superclass in the constant pool.

If the value of super_class is zero, then the class being defined must be java.lang.Object, and it has no superclass.

interfaces_count

This field gives the number of interfaces that this class implements.

interfaces

Each value in this table is an index into the constant pool. If an table value is nonzero (interfaces[i] != 0, where 0 <= i < interfaces_count), then constant_pool[interfaces[i]] must be an interface that this class implements.

Question: How could one of these entries ever be 0?

fields_count

This field gives the number of instance variables, both static and dynamic, defined by this class. The fields table includes only those variables that are defined explicitly by this class. It does not include those instance variables that are accessible from this class but are inherited from superclasses.

fields

Each value in this table is a more complete description of a field in the class. See section 2.4 for more information on the field_info structure.

methods_count

This field indicates the number of methods, both static and dynamic, defined by this class. This table only includes those methods that are explicitly defined by this class. It does not include inherited methods.

methods

Each value in this table is a more complete description of a method in the class. See section 2.5 for more information on the method_info structure.

attributes_count

This field indicates the number of additional attributes about this class.

attributes

A class can have any number of optional attributes associated with it. Currently, the only class attribute recognized is the "SourceFile" attribute, which indicates the name of the source file from which this class file was compiled. See section 2.6 for more information on the attribute_info structure.


Contents Prev Next Up