After the interfaces come the class’s fields, then its methods. Each list is a two-byte count followed by that many entries, one after another. Fields and methods have the same shape: flags, a name, a descriptor and a list of attributes. Only the members the class declares itself are listed; inherited ones are not.
field_info
fields_count (u2) is followed by that many field_info
structures:
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 2 | u2 | access_flags | The field flags |
| 2 | 2 | u2 | name_index | A Utf8 entry: the field’s simple name |
| 4 | 2 | u2 | descriptor_index | A Utf8 entry: a field descriptor, such as I or Ljava/lang/String; |
| 6 | 2 | u2 | attributes_count | The number of attributes |
| 8 | varies | attribute_info[] | attributes | The field’s attributes |
No two fields of a class may have the same name and descriptor. Two fields may share a name if their descriptors differ, which the Java language never does but the format allows.
Attributes a field may carry:
- ConstantValue: the
value of a
finalfield whose initialiser is a compile-time constant; the JVM uses it only for static fields; - Synthetic, Deprecated;
- Signature: the generic type, when it has one;
- RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleTypeAnnotations and RuntimeInvisibleTypeAnnotations.
method_info
methods_count (u2) is followed by that many method_info
structures. Constructors, the class initialiser and compiler-made methods are all
methods here.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 2 | u2 | access_flags | The method flags |
| 2 | 2 | u2 | name_index | A Utf8 entry: the method’s simple name, or <init> or <clinit> |
| 4 | 2 | u2 | descriptor_index | A Utf8 entry: a method descriptor, such as ([Ljava/lang/String;)V |
| 6 | 2 | u2 | attributes_count | The number of attributes |
| 8 | varies | attribute_info[] | attributes | The method’s attributes |
No two methods of a class may have the same name and descriptor. Methods that differ only in their return type are allowed by the format; the compiler makes such pairs for bridge methods.
Every method that is neither abstract nor native has exactly
one Code attribute, and abstract and native
methods have none. The other attributes a method may carry:
- Exceptions: the
checked exceptions in its
throwsclause; - MethodParameters:
parameter names and flags (written with
javac -parameters); - RuntimeVisibleParameterAnnotations and RuntimeInvisibleParameterAnnotations;
- AnnotationDefault: the default of an annotation interface’s element;
- Synthetic, Deprecated, Signature;
- the four annotation and type annotation attributes, as for fields.
Special methods
| Name | What it is | Rules |
|---|---|---|
<init> | A constructor (an instance initialisation method). One for each constructor in the source; the compiler adds a no-argument one when the source has none. | Returns V. Called only by invokespecial on a new object or, from another constructor, on this. |
<clinit> | The class or interface initialiser: the static initialisers and static field assignments, in source order. | Descriptor ()V; ACC_STATIC from 51.0. Never called by bytecode; the JVM runs it once, before the class is first used. |
The compiler also makes methods of its own and marks them ACC_SYNTHETIC: lambda
bodies such as lambda$main$0, bridge methods for generics, and before Java
11 access methods such as access$000 that let nested classes reach each
other’s private members. The language side of these is on the
Java classes and
generics pages.
In the explorer
The explorer names each field and method after itself and shows it as a Java
declaration, built from its flags and descriptor, such as
public static void main(java.lang.String[]) or
private final int count. A descriptor that is not valid is marked.
An example method
The only method of java/util/EmptyStackException.class (JDK 1.1.4), at
offset 0xCF, after methods_count 00 01:
000000C0 00
000000D0 01 00 06 00 05 00 01 00 07 00 00 00 11 00 01 00
000000E0 01 00 00 00 05 2A B7 00 03 B1 00 00 00 00
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 0xCF | 00 01 | access_flags | ACC_PUBLIC |
| 0xD1 | 00 06 | name_index | #6, <init> |
| 0xD3 | 00 05 | descriptor_index | #5, ()V |
| 0xD5 | 00 01 | attributes_count | 1 |
| 0xD7 | 00 07 00 00 00 11 … | attributes | a Code attribute of 17 bytes |
The explorer shows it as public java.util.EmptyStackException().