Java Quick Guide

 
 
This guide contains useful Java information.

rss feed






Java class file format.



Information
This tutorial documents the Java class file format. Class files are used to hold compiled versions of both Java classes and Java Interfaces. Compliant Java interpreters must be capable of dealing with all class files that conform to the class file specification. A Java .class file consists of a stream of 8-bit bytes. All 16-bit and 32-bit quantities are constructed by reading in two or four 8-bit bytes, respectively. The bytes are joined together in big-endian order.

More information about the Java Class file can be found at: http://java.sun.com/docs/books/jvms/
The latest Java Class File Specification can be found at: http://jcp.org/en/jsr/detail?id=202
An online Java class decompiler can be found at:
http://www.mobilefish.com/services/java_decompiler/java_decompiler.php

This tutorial is based on:
JSR-000202 Java(tm) Class File Specification Update ("Specification")
Version: 0.2
Status: Proposed Final Draft
Release: 2 October 2006


Download the JSR-000202 Java Class File Specification.

In this tutorial, we use notation u1, u2, and u4 to mean an unsigned one-, two-, or four-byte quantity, respectively. B1, B2, B3 means Byte 1, Byte 2 and Byte 3, respectively.

The Java class file structure (see also Table 1):

classfile
{
   u4 magic field;
   u2 minor version;
   u2 major version;
File Header
   u2 constant pool table count;
         constant pool table[constant pool table count - 1];
Constant Pool
   u2 access flags;
   u2 this class;
   u2 super class;
   u2 interfaces count;
   u2 interfaces[interfaces count];
Class Descriptor
   u2 fields count;
         fields[fields count];
Fields
   u2 methods count
         methods[methods count];
Methods
   u2 attributes count;
         attributes[attributes count];
Attributes
}



Table 1: Class file
B1 B2 B3 B4 # of bytes Description
X X X X u4 Magic field
byte range: 0..3

The magic field must have the value 0xCAFEBABE.
It is used to identify this file as conforming to the class file format.

X X     u2 Minor version
byte range: 4..5

The minor version number of the Java compiler that produced this class file.

X X     u2 Major version
byte range: 6..7

The major version number of the Java compiler that produced this class file.

Table 2: Major and Minor version versus JVM
minor (decimal) major (decimal) JVM JDK
0<= minor <=65535 45 <= major<=51 1.7 Java SE 7
0<= minor <=65535 45 <= major<=50 1.6 Java SE 6
0<= minor <=65535 45 <= major<=49 1.5 J2SE 1.5
0<= minor <=65535 45 <= major<=48 1.4 J2SE 1.4
0<= minor <=65535 45 <= major<=47 1.3 J2SE 1.3
0<= minor <=65535 45 <= major<=46 1.2 J2SE 1.2
0<= minor <=65535 major==45 1.1 JDK 1.1.x
0<= minor <=3 major==45 1.0.2 JDK 1.0


For example:
Minor = 0 and major = 48 means that the class file is created by JVM 1.4.
This class file can also be executed on JVM 1.5, 1.6 and 1.7

X X     u2 Constant pool table count
byte range: 8..9

Number of entries (cp_count) in the constant pool table.

.. .. .. .. cpsize Constant pool table
byte range: 10..(cpsize-1)

The constant pool table (cp_table) is an array of values, containing string constants, class names, field and method names, and others that are referred to by the class structure or by the code.

The constant pool table structure can be found in Table 3.
The byte size of the constant pool table is variable, indicated by cpsize.

For example:
If the constant pool table count is 72, it means that there are 72 "array" records, starting with index cp_table[0] and ending with cp_table[71].
Please note: cp_table[0] is ALWAYS empty.

X X     u2 Access flags
byte range: (10+cpsize)..(11+cpsize)

This field contains a bitmask used to determine the access specifier of the class.

Table 4: Class access specifiers
Value (hex) Name Access specifier Description
0x0001 ACC_PUBLIC public Visible to everyone
0x0010 ACC_FINAL final No subclasses allowed
0x0020 ACC_SUPER   Treat superclass methods specially when invoked by the invokespecial instruction.
0x0200 ACC_INTERFACE interface Is an interface, not a class
0x0400 ACC_ABSTRACT abstract Must not be instantiated
0x1000 ACC_SYNTHETIC   Generated by the compiler and does not appear in the source code.
0x2000 ACC_ANNOTATION annotation  
0x4000 ACC_ENUM enum This class or its superclass is declared as an enumerated type.

For example:
public abstract class AbstractConfiguration {..}

The access flags value is 0x0401, which is a public abstract class file.

X X     u2 this class
byte range: (12+cpsize)..(13+cpsize)

This field is an index into the constant pool table and refers to a Class Reference type. If this Class Reference is resolved you will get the fully qualified name of the current class.

For example:
package org.apache.commons.configuration;
public abstract class AbstractConfiguration {..}


The fully qualified name of this class is:
org/apache/commons/configuration/AbstractConfiguration class file.

X X     u2 super class
byte range: (14+cpsize)..(15+cpsize)

This field is an index into the constant pool table and refers to a Class Reference type. If this Class Reference is resolved you will get the fully qualified name of the super class. If this field is 0, then this class has no super class, it will refer to java/lang/Object.

For example:
import org.apache.commons.mail.Email;
public class MailHandler extends Email {..}


The fully qualified name of the super class is org/apache/commons/mail/Email class file.

X X     u2 Interfaces count
byte range: (16+cpsize)..(17+cpsize)

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

For example:
import com.mobilefish.interfaces.CompanyHandler;
import com.mobilefish.interfaces.ProfileHandler;
public class CompanyProfileHandlerImpl implements CompanyHandler, ProfileHandler {..}


In this example interfaces count is 2.

.. .. .. .. isize Interfaces[]
byte range: (18+cpsize)..(18+cpsize+isize-1)

The byte size of the interfaces array is variable, indicated by isize.

The interfaces array is a single dimensional array where:

interfaces[interface_count]
{
   u2 class_reference;
}
  • The first two bytes must be an index into the constant_pool table referring to a Class Reference type.

    For example:
    import com.mobilefish.interfaces.CompanyHandler;
    import com.mobilefish.interfaces.ProfileHandler;
    public class CompanyProfileHandlerImpl implements CompanyHandler, ProfileHandler {..}


    interface_count = 2

    interfaces[0] = 0x0f45. This index refers to a Class Reference, when resolved it refers to e.g.:
    com/mobilefish/interfaces/CompanyHandler

    interfaces[1] = 0x10ff. This index refers to a Class Reference, when resolved it refers to e.g.:
    com/mobilefish/interfaces/ProfileHandler

    In this example isize = interface_count * u2 = 2 * u2 = u4
X X     u2 Fields count
byte range: (18+cpsize+isize)..(19+cpsize+isize)

This field gives the number of instance variables, both static and dynamic defined by this class (fields_count). The fields array 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.

For example:
public class Configuration {
   protected static final String ON = "1";
   private boolean stop = false;
   :
}


In this example fields count is 2.

.. .. .. .. fsize Fields[]
byte range: (20+cpsize+isize)..(20+cpsize+isize+fsize-1)

The byte size of the fields array is variable, indicated by fsize.

The fields array is a multi dimensional array where:

fields[fields_count]
{
   u2 access_flags;
   u2 name_index;
   u2 descriptor_index;
   u2 attributes_count;
      attributes[attributes_count]
      {
         u2 attribute_name_index;
         u4 attribute_length;
            info[attribute_length]
            {
               [attribute data, see Table 9]
            }
      }
}


For example:
public class Configuration {
   protected static final String ON = "1";
   private boolean stop;
   :
}


In this example fields count is 2.

fields[0]['access flags'] = 0x001c (= protected final static)
fields[0]['name index'] = 0x2c2d.This index resolves into field name ON.
fields[0]['descriptor index'] = 0x2c44. This index resolves into field type descriptor Ljava/lang/String;.
fields[0]['attributes count'] = 1
fields[0][0]['attribute name index'] = 0x20b3. This index resolves into attribute name ConstantValue.
fields[0][0]['attribute length'] = 2
fields[0][0]['constant_index'] = 0x2d1c. This index resolves into constant value "1".

fields[1]['access flags'] = 0x0002 (= private)
fields[1]['name index'] = 0x2f02.This index resolves into field name stop.
fields[1]['descriptor index'] = 0x2e56. This index resolves into field type descriptor Z.
fields[1]['attributes count'] = 0

Hide details
  • access_flags

    The first 2 bytes contains a bitmask used to determine the access flags.

    Table 5: Field access specifiers
    Value (hex) Name Access specifier Description
    0x0001 ACC_PUBLIC public Visible to everyone.
    0x0002 ACC_PRIVATE private Usable only within the defining class.
    0x0004 ACC_PROTECTED protected May be accessed within subclasses.
    0x0008 ACC_STATIC static Variable is static
    0x0010 ACC_FINAL final No further assignment after initialization.
    0x0040 ACC_VOLATILE volatile Cannot be cached.
    0x0080 ACC_TRANSIENT transient Not written or read by a persistent object manager.
    0x1000 ACC_SYNTHETIC   Generated by the compiler and does not appear in the source code.
    0x4000 ACC_ENUM enum  

    For example:
    Java statement: private static char myValue = 'A';
    This field must have value 0x000A, meaning this field is private static.

  • name_index

    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 of the Name and Type descriptor type refers to a UTF8 type which resolves to a field name.

    For example:
    Java statement: private static char myValue = 'A';
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 refers to a UTF8 type which resolves to field name: myValue.

  • descriptor_index

    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 of the Name and Type descriptor type refers to a UTF8 type which resolves to a field type descriptor.

    For example:
    Java statement 1: byte myByte = 5;
    Java statement 2: char myChar = 'A';
    Java statement 3: double myDouble = 13.4d;
    Java statement 4: float myFloat = 5.8f;
    Java statement 5: int myInt = 5;
    Java statement 6: long myLong = 10;
    Java statement 7: String myString = "Hello";
    Java statement 8: short myShort = 9;
    Java statement 9: boolean myBoolean = true;
    Java statement 10:int[] myArr = {5,7};
    Java statement 11:char[ ][ ] myTable = new char[4][3];

    In case of statement 1:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: B.

    In case of statement 2:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: C.

    In case of statement 3:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: D.

    In case of statement 4:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: F.

    In case of statement 5:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: I.

    In case of statement 6:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: J.

    In case of statement 7:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: Ljava/lang/String;.

    In case of statement 8:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: S.

    In case of statement 9:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: Z.

    In case of statement 10:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: [I.

    In case of statement 11:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to field type descriptor: [[C.

    As you may notice the field type descriptors must in someway correspond to char, byte etc. See Table 6.

    Table 6: Field type descriptor
    Field type descriptor Type Interpretation
    B byte signed byte
    C char Unicode character
    D double double-precision floating-point value
    F float single-precision floating-point value
    I int integer
    J long long integer
    LClassname; reference an instance of class <classname>
    S short signed short
    Z boolean true or false
    [ reference one array dimension

  • attributes_count

    The next two bytes gives the number of attributes (attributes_count).

  • attributes[]

    A field can have any number of attributes associated with it.

  • attribute_name_index

    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 of the Name and Type descriptor type refers to a UTF8 type which resolves to the attribute name. Certain attributes names are predefined as part of the class file specification. These predefined atribute names which the UTF8 type resolves into can be found in the following table.

    Table 7: Field predefined attribute names
    Predefined attribute name Description Required / Optional
    ConstantValue The ConstantValue attribute represents the value of a constant field.

    Note: attributes_count is always 1.
    Required***
    Synthetic The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces.

    A class member that does not appear in the source code must be marked using a Synthetic attribute, or else it must have its ACC_SYNTHETIC bit set. The only exceptions to this requirement are for default constructors and the class initialization method.

    Required**
    Signature   Optional*.
    Deprecated The Deprecated attribute was introduced in JDK release 1.1 to support the @deprecated tag in documentation comments.

    A class, interface, method, or field may be marked using a Deprecated attribute to indicate that the class, interface, method, or field has been superseded. A runtime interpreter or tool that reads the class file format, such as a compiler, can use this marking to advise the user that a superseded class, interface, method, or field is being referred to. The presence of a Deprecated attribute does not alter the semantics of a class or interface.
    Optional*
    RuntimeVisible- Annotations The RuntimeVisibleAnnotations attribute records runtime-visible Java programming language annotations on the corresponding class, method, or field. Each ClassFile, field_info, and method_info structure may contain at most one RuntimeVisibleAnnotations attribute, which records all the runtime-visible Java programming language annotations on the corresponding program element. The JVM must make these annotations available so they can be returned by the appropriate reflective APIs. Required**
    RuntimeInvisible- Annotations The RuntimeInvisibleAnnotations attribute records runtime-invisible Java programming language annotations on the corresponding class, method, or field. Each ClassFile, field_info, and method_info structure may contain at most one RuntimeInvisibleAnnotations attribute, which records all the runtime-invisible Java programming language annotations on the corresponding program element. Required**
    *) A class file reader may use the information they contain, or otherwise must silently ignore those attributes.
    **) In order to properly implement the Java and Java 2 platform class libraries.
    ***) For correct interpretation of the class file by a Java virtual machine implementation.

  • attribute_length

    The next 4 bytes indicates the length of the subsequent information in bytes. This is not the same as a count!

    Explanation attribute_length:
    The attribute_name_index is stored in 2 bytes.
    The attribute_length is stored in 4 bytes.
    If attribute_length value = 100, info[] is stored in 100 bytes.

    Table 8: Field predefined attribute names and attribute_length
    Predefined attribute name attribute_length value
    ConstantValue 2
    Synthetic 0
    Signature 2
    Deprecated 0
    RuntimeVisibleAnnotations variable
    RuntimeInVisibleAnnotations variable


  • info[]

    Info array contains the attribute data for each predefined attribute name.

    Table 9: Field predefined attribute names and attribute data
    Predefined attribute name attribute data
    ConstantValue u2 constant_index;

    The next two bytes must be an index into the constant pool table referring to an Integer, Float, Long, Double or String Ref type.
    Synthetic No data stored.
    Signature u2 signature_index;

    The next two bytes must be an index into the constant pool table referring to a UTF8 type which resolves to a field type signature.
    Deprecated No data stored.
    RuntimeVisible- Annotations u2 num_annotations;
       annotations[num_annotations]
       {
          u2 type_index;
          u2 num_element_value_pairs;
             element_value_pairs
             [num_element_value_pairs]
             {
               u2 element_name_index;
                  element_value value;
             }
       }


    num_annotations
    The value of the num_annotations item gives the number of runtime-visible annotations represented by the structure. Note that a maximum of 65535 runtimevisible Java programming language annotations may be directly attached to a program element.

    annotations
    Each value of the annotations table represents a single runtime-visible annotation on a program element.

    type_index
    The value of the type_index item must be a valid index into the constant pool table. The constant pool entry at that index must be a UTF8 type representing a field descriptor representing the annotation type corresponding to the annotation represented by this annotation structure.

    num_element_value_pairs
    The value of the num_element_value_pairs item gives the number of elementvalue pairs of the annotation represented by this annotation structure. Note that a maximum of 65535 element-value pairs may be contained in a single annotation.

    element_value_pairs
    Each value of the element_value_pairs table represents a single element-value pair in the annotation represented by this annotation structure. Each element_value_pairs entry contains the following two items element_name_index and element_value value.

    element_name_index
    The value of the element_name_index item must be a valid index into the constant pool table. The constant pool entry at that index must be a UTF8 type representing the name of the annotation type element represented by this element_value_pairs entry.

    value
    The value of the value item represents the value of the element-value pair represented by this element_value_pairs entry.

    RuntimeInVisible- Annotations See RuntimeVisibleAnnotations




X X     u2 Methods count
byte range: (20+cpsize+isize+fsize)..(21+cpsize+isize+fsize)

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

For example:
private char delimiter;

public void setDelimiter(char c) {
   delimiter = c;
}

public char getDelimiter() {
   return delimiter;
}


In this example methods count is 2.

.. .. .. .. msize Methods[]
byte range: (22+cpsize+isize+fsize)..(22+cpsize+isize+fsize+msize-1)

The byte size of the methods array is variable, indicated by msize.

The methods array is a multi dimensional array where:

methods[methods_count]
{
   u2 access_flags;
   u2 name_index;
   u2 descriptor_index;
   u2 attributes_count;
      attributes[attributes_count]
      {
         u2 attribute_name_index;
         u4 attribute_length;
            info[attribute_length]
            {
               [attribute data, see Table 13]
            }
      }
}


For example:
private char delimiter;

public void setDelimiter(char c) {
   delimiter = c;
}

public char getDelimiter() {
   return delimiter;
}


In this example methods count is 2.

methods[0]['access flags'] = 0x0001 (= public)
methods[0]['name index'] = 0x1c2d.This index resolves into method name setDelimiter.
methods[0]['descriptor index'] = 0x1c44. This index resolves into method type descriptor (C)V.
methods[0]['attributes count'] = 1
methods[0][0]['attribute name index'] = 0x00b3. This index resolves into attribute name Code.
methods[0][0]['attribute length'] = 51
methods[0][0]['max stack'] = 1
methods[0][0]['max locals'] = 2
methods[0][0]['code length'] = 5
methods[0][0]['code'] = 1a, b3, 00, 17, b1
methods[0][0]['exception table length'] = 0
methods[0][0]['code attributes count'] = 2

methods[1]['access flags'] = 0x0001 (= public)
methods[1]['name index'] = 0x1f02.This index resolves into method name getDelimiter.
methods[1]['descriptor index'] = 0x1e56. This index resolves into method type ()C.
methods[1]['attributes count'] = 1
methods[1][0]['attribute name index'] = 0x30b3. This index resolves into attribute name Code.
methods[1][0]['attribute length'] = 36
methods[1][0]['max stack'] = 1
methods[1][0]['max locals'] = 0
methods[1][0]['code length'] = 4
methods[1][0]['code'] = b2, 00, 17, ac
methods[1][0]['exception table length'] = 0
methods[1][0]['code attributes count'] = 2


Hide details
  • access_flags

    The first 2 bytes contains a bitmask used to determine the access flags.

    Table 10: Method access specifiers
    Value (hex) Name Access specifier Description
    0x0001 ACC_PUBLIC public Visible to everyone.
    0x0002 ACC_PRIVATE private Accessible only within the defining class.
    0x0004 ACC_PROTECTED protected May be accessed within subclasses.
    0x0008 ACC_STATIC static Method is static
    0x0010 ACC_FINAL final Must not be overridden
    0x0020 ACC_SYNCHRONIZED synchronized Invocation is wrapped in a monitor lock.
    0x0040 ACC_BRIDGE   A bridge method, generated by the compiler.
    0x0080 ACC_VARARGS   Declared with variable number of arguments.
    0x0100 ACC_NATIVE native Implemented in a language other than Java.
    0x0400 ACC_ABSTRACT abstract No implementation is provided.
    0x0800 ACC_STRICT strictfp Floating-point mode is FP-strict.
    0x1000 ACC_SYNTHETIC   Generated by the compiler and does not appear in the source code.

    For example:
    Java statement: private static char getValue();
    This field must have value 0x000A, meaning this method is private static.

  • name_index

    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 of the Name and Type descriptor type refers to a UTF8 type which resolves to a method name.

    For example:
    Java statement: private static char getValue();
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 refers to a UTF8 type which resolves to method name: getValue().

    Note:
    There are two special method names: <init> or <clinit>.
    <init> represents an instance initialization method. The return type of such a method must be void.
    Class initialisation code goes in a method called <clinit> (no arguments, void return type).

  • descriptor_index

    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 of the Name and Type descriptor type refers to a UTF8 type which resolves to a method type descriptor.

    For example:
    Java statement 1: Object myMethod1(int i, double d, Thread t);
    Java statement 2: void myMethod2(char c, float f, long l);
    Java statement 3: void myMethod3(String s, short k, boolean b);
    Java statement 4: String[][]myMethod4(int[][] i, double d);

    In case of statement 1:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to method type descriptor:
    (IDLjava/lang/Thread;)Ljava/lang/Object;.

    In case of statement 2:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to method type descriptor: (CFJ)V.

    In case of statement 3:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to method type descriptor: (Ljava/lang/String;SZ)V.

    In case of statement 4:
    The two bytes are an index into the constant pool table referring to a Name and Type descriptor type. Bytes X3 and X4 refers to a UTF8 type which resolves to method type descriptor:
    ([[ID)[[Ljava/lang/String;.

    As you may notice the method type descriptors must in someway correspond to char, byte etc. See Table 17.

    Table 17: Method type descriptor
    Method type descriptor Type Interpretation
    B byte signed byte
    C char Unicode character
    D double double-precision floating-point value
    F float single-precision floating-point value
    I int integer
    J long long integer
    LClassname; reference an instance of class <classname>
    S short signed short
    Z boolean true or false
    [ reference one array dimension
    V void The character V indicates that the method returns no value (its return type is void)


  • attributes_count

    The next two bytes gives the number of attributes (attributes_count).

  • attributes[]

    A method can have any number of optional attributes associated with it.

  • attribute_name_index

    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 of the Name and Type descriptor type refers to a UTF8 type which resolves to the attribute name. Certain attributes names are predefined as part of the class file specification. These predefined atribute names which the UTF8 type resolves into can be found in the following table.

    Table 11: Method predefined attribute names
    Predefined attribute name Description Required / Optional
    Code A Code attribute contains the Java virtual machine instructions and auxiliary information for a single method, instance initialization method or class or interface initialization method.

    Note:
    If the method is native or abstract, attributes_count=0, otherwise attributes_count=1.
    Required***
    Exceptions The Exceptions attribute indicates which checked exceptions a method may throw.

    Note:
    attributes_count=0 or 1.
    Required***
    Deprecated The Deprecated attribute was introduced in JDK release 1.1 to support the @deprecated tag in documentation comments.

    A class, interface, method, or field may be marked using a Deprecated attribute to indicate that the class, interface, method, or field has been superseded. A runtime interpreter or tool that reads the class file format, such as a compiler, can use this marking to advise the user that a superseded class, interface, method, or field is being referred to. The presence of a Deprecated attribute does not alter the semantics of a class or interface.
    Optional*
    Synthetic The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces.

    A class member that does not appear in the source code must be marked using a Synthetic attribute, or else it must have its ACC_SYNTHETIC bit set. The only exceptions to this requirement are for default constructors and the class initialization method.
    Required**
    Signature   Optional*
    RuntimeVisible- Annotations See Table 7 Required**
    RuntimeInvisible- Annotations See Table 7 Required**
    RuntimeVisible- ParameterAnnotations The RuntimeVisible- ParameterAnnotations attribute records runtime-visible Java programming language annotations on the parameters of the corresponding method. Each method_info structure may contain at most one RuntimeVisible- ParameterAnnotations attribute, which records all the runtime-visible Java programming language annotations on the parameters of the corresponding method. The JVM must make these annotations available so they can be returned by the appropriate reflective APIs. Required**
    RuntimeInvisible- ParameterAnnotations The RuntimeInvisible- ParameterAnnotations attribute records runtime-invisible Java programming language annotations on the parameters of the corresponding method. Each method_info structure may contain at most one RuntimeInvisible- ParameterAnnotations attribute, which records all the runtime-invisible Java programming language annotations on the parameters of the corresponding method. Required**
    AnnotationDefault The AnnotationDefault attribute records the default value for the element represented by the method_info structure. Each method_info structures representing an element of an annotation types may contain at most one AnnotationDefault attribute. The JVM must make this default value available so it can be applied by appropriate reflective APIs. Required**
    *) A class file reader may use the information they contain, or otherwise must silently ignore those attributes.
    **) In order to properly implement the Java and Java 2 platform class libraries.
    ***) For correct interpretation of the class file by a Java virtual machine implementation.

  • attribute_length

    The next 4 bytes indicates the length of the subsequent information in bytes. This is not the same as a count!

    Explanation attribute_length:
    The attribute_name_index is stored in 2 bytes.
    The attribute_length is stored in 4 bytes.
    If attribute_length value = 100, info[] is stored in 100 bytes.

    Table 12: Method predefined attribute names and attribute_length
    Predefined attribute name attribute_length value
    Code variable
    Exceptions variable
    Deprecated 0
    Synthetic 0
    Signature 2
    RuntimeVisibleAnnotations variable
    RuntimeVisibleParameterAnnotations variable
    RuntimeInVisibleParameterAnnotations variable
    AnnotationDefault variable


  • info[]

    Info array contains the attribute data for each predefined attribute name.

    Table 13: Method predefined attribute names and attribute data
    Predefined attribute name attribute data
    Code u2 max_stack;
    u2 max_locals;
    u4 code_length;
    u1 code[code_length];
    u2 exception_table_length;
       exception_table[exception_table_length]
       {
          u2 start_pc;
          u2 end_pc;
          u2 handler_pc;
          u2 catch_type;
       }
       u2 code_attributes_count;
          code_attributes_table
          [code_attributes_count]
          {
             u2 code_attribute_name_index;
             u4 code_attribute_length;
                code_info[code_attribute_length]
                {
                   [attribute data,
                   see Table 20]
                }
          }


    max_stack
    The value of the max_stack item gives the maximum depth of the operand stack of this method at any point during execution of the method. Max_stack is an integer in the interval from 0 to 65535 (both inclusive).

    max_locals
    The value of the max_locals item gives the number of local variables in the local variable array allocated upon invocation of this method, including the local variables used to pass parameters to the method on its invocation. The greatest local variable index for a value of type long or double is max_locals-2. The greatest local variable index for a value of any other type is max_locals-1.

    code_length
    The value of the code_length item gives the number of bytes in the code array for this method. The value of code_length must be greater than zero; the code array must not be empty.

    code[]
    The code array gives the actual bytes of Java virtual machine code that implement the method. When the code array is read into memory on a byteaddressable machine, if the first byte of the array is aligned on a 4-byte boundary, the tableswitch and lookupswitch 32-bit offsets will be 4-byte aligned.

    exception_table_length
    The value of the exception_table_length item gives the number of entries in the exception_table table.

    start_pc
    The values of the two items start_pc and end_pc indicate the ranges in the code array at which the exception handler is active. The value of start_pc must be a valid index into the code array of the opcode of an instruction. The value of end_pc either must be a valid index into the code array of the opcode of an instruction or must be equal to code_length, the length of the code array. The value of start_pc must be less than the value of end_pc. The start_pc is inclusive and end_pc is exclusive; that is, the exception handler must be active while the program counter is within the interval [start_pc, end_pc).

    end_pc
    See start_pc.

    handler_pc
    The value of the handler_pc item indicates the start of the exception handler. The value of the item must be a valid index into the code array and must be the index of the opcode of an instruction.

    catch_type
    If the value of the catch_type item is nonzero, it must be a valid index into the constant pool table. The constant pool entry at that index must be a Class Reference representing a class of exceptions that this exception handler is designated to catch. The exception handler will be called only if the thrown exception is an instance of the given class or one of its subclasses. If the value of the catch_type item is zero, this exception handler is called for all exceptions. This is used to implement finally.
    Exceptions u2 number_of_exceptions;
    u2 exception_index_table
       [number_of_exceptions];


    number_of_exceptions
    The value of the next two bytes indicates the number of entries in the exception_index_table.

    exception_index_table
    Each value in the exception_index_table array must be a valid index into the constant pool table. The constant pool entry referenced by each table item must be a Class Reference representing a class type that this method is declared to throw.

    Deprecated No data stored.
    Synthetic No data stored.
    Signature u2 signature_index;

    The next two bytes must be an index into the constant pool table referring to a UTF8 type which resolves to a method type signature.
    RuntimeVisible- Annotations See Table 9
    RuntimeInVisible- Annotations See Table 9
    RuntimeVisible- ParameterAnnotations u1 num_parameters;
       parameter_annotations[num_parameters]
       {
          u2 num_annotations;
             annotation annotations[num_annotations];
       }


    num_parameters
    The value of the num_parameters item gives the number of parameters of the method represented by the method_info structure on which the annotation occurs. (This duplicates information that could be extracted from the method descriptor.)

    parameter_annotations
    Each value of the parameter_annotations table represents all of the runtimeinvisible annotations on a single parameter. The sequence of values in the table corresponds to the sequence of parameters in the method signature. Each parameter_annotations entry contains the following two items: num_annotations and annotation annotations.

    num_annotations
    The value of the num_annotations item indicates the number of runtimeinvisible annotations on the parameter corresponding to the sequence number of this parameter_annotations element.

    annotation annotations
    Each value of the annotations table represents a single runtime-invisible annotation on the parameter corresponding to the sequence number of this parameter_annotations element.

    RuntimeVisible- ParameterAnnotations See RuntimeVisibleParameterAnnotations
    AnnotationDefault element_value default_value;

    element_value default_value
    The default_value item represents the default value of the annotation type element whose default value is represented by this AnnotationDefault attribute.



    code_attributes_table

    • code_attributes_count

      The next two bytes gives the number of attributes (code_attributes_count) in the code_attributes_table.

    • code_attribute_name_index

      The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 of the Name and Type descriptor type refers to a UTF8 type which resolves to the code attribute name. Certain code attributes names are predefined as part of the class file specification. These predefined atribute names which the UTF8 type resolves into can be found in the following table.

      Table 18: Code predefined attribute names
      Predefined attribute name Description Required / Optional
      StackMapTable This attribute is used during the process of verification by typechecking. Required*** for major version 50.0 or above.
      LineNumberTable Contains debugging information.

      If LineNumberTable attributes are present in the attributes table of a given Code attribute, then they may appear in any order. Furthermore, multiple LineNumberTable attributes may together represent a given line of a source file; that is, LineNumberTable attributes need not be one-to-one with source lines.
      Optional*
      LocalVariableTable Contains debugging information.

      It may be used by debuggers to determine the value of a given local variable during the execution of a method. If LocalVariableTable attributes are present in the attributes table of a given Code attribute, then they may appear in any order. There may be no more than one LocalVariableTable attribute per local variable in the Code attribute.
      Optional*
      LocalVariableTypeTable Contains debugging information.

      It may be used by debuggers to determine the value of a given local variable during the execution of a method. If LocalVariableTypeTable attributes are present in the attributes table of a given Code attribute, then they may appear in any order. There may be no more than one LocalVariableTypeTableattribute per local variable in the Code attribute. The LocalVariableTypeTable attribute differs from the LocalVariableTable attribute in that it provides signature information rather than descriptor information. This difference is only significant for variables whose type is a generic reference type. Such variables will appear in both tables, while variables of other types will appear only in LocalVariableTable.
      Optional*
      *) A class file reader may use the information they contain, or otherwise must silently ignore those attributes.
      ***) For correct interpretation of the class file by a Java virtual machine implementation.

    • code_attribute_length

      The next 4 bytes indicates the length of the subsequent information in bytes. This is not the same as a count!

      Explanation code_attribute_length:
      The code_attribute_name_index is stored in 2 bytes.
      The code_attribute_length is stored in 4 bytes.
      If code_attribute_length value = 100, code_info[] is stored in 100 bytes.

      Table 19: Code predefined attribute names and attribute_length
      Predefined attribute name code_attribute_length value
      StackMapTable variable
      LineNumberTable variable
      LocalVariableTable variable
      LocalVariableTypeTable variable

    • code_info[]

      Code_info array contains the attribute data for each predefined attribute name.

      Table 20: Code predefined attribute names and attribute data
      Predefined attribute name attribute data
      StackMapTable @@@
      LineNumberTable u2 line_number_table_length;
         line_number_table
         [line_number_table_length]
         {
            u2 start_pc;
            u2 line_number;
         }


      line_number_table_length
      The value of the line_number_table_length item indicates the number of entries in the line_number_table array.

      line_number_table
      Each entry in the line_number_table array indicates that the line number in the original source file changes at a given point in the code array.

      start_pc
      The value of the start_pc item must indicate the index into the code array at which the code for a new line in the original source file begins. The value of start_pc must be less than the value of the code_length item of the Code attribute of which this LineNumberTable is an attribute.

      line_number
      The value of the line_number item must give the corresponding line number in the original source file.

      LocalVariableTable u2 local_variable_table_length;
         local_variable_table
         [local_variable_table_length]
         {
            u2 start_pc;
            u2 length;
            u2 name_index;
            u2 descriptor_index;
            u2 index;
         }


      local_variable_table_length
      The value of the local_variable_table_length item indicates the number of entries in the local_variable_table array.

      local_variable_table[]
      Each entry in the local_variable_table array indicates a range of code array offsets within which a local variable has a value. It also indicates the index into the local variable array of the current frame at which that local variable can be found.

      start_pc, length
      The given local variable must have a value at indices into the code array in the interval [start_pc, start_pc+length), that is, between start_pc and start_pc+length exclusive. The value of start_pc must be a valid index into the code array of this Code attribute and must be the index of the opcode of an instruction. The value of start_pc+length must either be a valid index into the code array of this Code attribute and be the index of the opcode of an instruction, or it must be the first index beyond the end of that code array.

      name_index, descriptor_index
      The value of the name_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must contain a UTF8 representing a valid unqualified name denoting a local variable. The value of the descriptor_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must contain a UTF8 representing a field descriptor encoding the type of a local variable in the source program.

      index
      The given local variable must be at index in the local variable array of the current frame. If the local variable at index is of type double or long, it occupies both index and index+1.

      LocalVariableTypeTable u2 local_variable_type_table_length;
         local_variable_type_table
         [local_variable_
         type_table_length]
         {
            u2 start_pc;
            u2 length;
            u2 name_index;
            u2 signature_index;
            u2 index;
         }


      local_variable_type_table_length
      The value of the local_variable_type_table_length item indicates the number of entries in the local_variable_table array.

      local_variable_type_table[]
      Each entry in the local_variable_type_table array indicates a range of code array offsets within which a local variable has a value. It also indicates the index into the local variable array of the current frame at which that local variable can be found.

      start_pc, length
      The given local variable must have a value at indices into the code array in the interval [start_pc, start_pc+length), that is, between start_pc and start_pc+length exclusive. The value of start_pc must be a valid index into the code array of this Code attribute and must be the index of the opcode of an instruction. The value of start_pc+length must either be a valid index into the code array of this Code attribute and be the index of the opcode of an instruction, or it must be the first index beyond the end of that code array.

      name_index, signature_index
      The value of the name_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must contain a UTF8 representing a valid unqualified name denoting a local variable. The value of the signature_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must contain a UTF8 type representing a field descriptor encoding the type of a local variable in the source program.

      index
      The given local variable must be at index in the local variable array of the current frame. If the local variable at index is of type double or long, it occupies both index and index+1.



X X     u2 Attributes count
byte range: (22+cpsize+isize+fsize+msize)..(23+cpsize+isize+fsize+msize)

This value gives the number of additional attributes about this class.

.. .. .. .. asize Attributes[]
byte range: (24+cpsize+isize+fsize+msize)..(24+cpsize+isize+fsize+msize+asize-1)

A class can have any number of optional attributes associated with it. The byte size of the attributes array is variable, indicated by asize. Attributes give additional information about the class. The "SourceFile" attribute gives the name of the source file from which this class was compiled.

attributes[attributes_count]
{
   u2 attribute_name_index;
   u4 attribute_length;
      info[attribute_length]
      {
         [attribute data, see Table 16]
      }
}


Hide details
  • attribute_name_index

    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type. Bytes X1 and X2 of the Name and Type descriptor type refers to a UTF8 type which resolves to the attribute name. Certain attributes names are predefined as part of the class file specification. These predefined atribute names which the UTF8 type resolves into can be found in the following table.

    Table 14: Class predefined attribute names
    Predefined attribute name Description Required / Optional
    SourceFile The SourceFile attribute contains the source filename of this class (e.g.: AbstractConfiguration.java)

    Note:
    attributes_count = 1.
    Optional*
    InnerClasses The InnerClasses attribute was introduced in JDK release 1.1 to support nested classes and interfaces. Required**
    EnclosingMethod A class must have an EnclosingMethod attribute if and only if it is a local class or an anonymous class. A class may have no more than one EnclosingMethod attribute.

    Optional*
    Synthetic The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces.

    A class member that does not appear in the source code must be marked using a Synthetic attribute, or else it must have its ACC_SYNTHETIC bit set. The only exceptions to this requirement are for default constructors and the class initialization method.
    Required**
    Signature   Optional*
    Deprecated The Deprecated attribute was introduced in JDK release 1.1 to support the @deprecated tag in documentation comments.

    A class, interface, method, or field may be marked using a Deprecated attribute to indicate that the class, interface, method, or field has been superseded. A runtime interpreter or tool that reads the class file format, such as a compiler, can use this marking to advise the user that a superseded class, interface, method, or field is being referred to. The presence of a Deprecated attribute does not alter the semantics of a class or interface.
    Optional*
    SourceDebugExtension There can be no more than one SourceDebugExtension attribute. Optional*
    RuntimeVisible- Annotations See Table 7 Required**
    RuntimeInvisible- Annotations See Table 7 Required**
    *) A class file reader may use the information they contain, or otherwise must silently ignore those attributes.
    **) In order to properly implement the Java and Java 2 platform class libraries.
    ***) For correct interpretation of the class file by a Java virtual machine implementation.

  • attribute_length

    The next 4 bytes indicates the length of the subsequent information in bytes. This is not the same as a count!

    Explanation attribute_length:
    The attribute_name_index is stored in 2 bytes.
    The attribute_length is stored in 4 bytes.
    If attribute_length value = 100, info[] is stored in 100 bytes.

    Table 15: Class predefined attribute names and attribute_length
    Predefined attribute name attribute_length value
    SourceFile 2
    InnerClasses variable
    EnclosingMethod 4
    Synthetic 0
    Signature 2
    Deprecated 0
    SourceDebugExtension variable
    RuntimeVisibleAnnotations variable
    RuntimeInVisibleAnnotations variable


  • info[]

    Info array contains the attribute data for each predefined attribute name.

    Table 16: Class predefined attribute names and attribute data
    Predefined attribute name attribute data
    SourceFile u2 sourcefile_index;

    The next two bytes must be an index into the constant pool table referring to a UTF8 type which resolves to the source filename.
    InnerClasses u2 number_of_classes;
       classes[number_of_classes]
       {
          u2 inner_class_info_index;
          u2 outer_class_info_index;
          u2 inner_name_index;
          u2 inner_class_access_flags;
       }


    number_of_classes
    The value of the number_of_classes item indicates the number of entries in the classes array.

    inner_class_info_index
    The value of the inner_class_info_index item must be zero or a valid index into the constant pool table. The constant pool entry at that index must be a Class Reference representing C. The remaining items in the classes array entry give information about C.

    outer_class_info_index
    If C is not a member, the value of the outer_class_info_index item must be zero. Otherwise, the value of the outer_class_info_index item must be a valid index into the constant_pool table, and the entry at that index must be a Class Reference representing the class or interface of which C is a member.

    inner_name_index
    If C is anonymous, the value of the inner_name_index item must be zero. Otherwise, the value of the inner_name_index item must be a valid index into the constant pool table, and the entry at that index must be UTF8 that represents the original simple name of C, as given in the source code from which this class file was compiled.

    inner_class_access_flags
    The value of the inner_class_access_flags item is a mask of flags used to denote access permissions to and properties of class or interface C as declared in the source code from which this class file was compiled. It is used by compilers to recover the original information when source code is not available. The flags are shown in Table 21.
    EnclosingMethod u2 class_index;
    u2 method_index;


    class_index
    The first two bytes must be an index into the constant pool table referring to a Class reference type. If this Class Reference is resolved you will get the the innermost class that encloses the declaration of the current class.

    method_index
    The next two bytes must be an index into the constant pool table referring to a Name and Type descriptor type, representing the name and type of a method in the class referenced by the class_index attribute above. It is the responsibility of the Java compiler to ensure that the method identified via the method_index is indeed the closest lexically enclosing method of the class that contains this EnclosingMethod attribute. If the current class is not immediately enclosed by a method or constructor, then the value of the method_index item must be zero.
    Synthetic No data stored.
    Signature u2 signature_index;

    The next two bytes must be an index into the constant pool table referring to a UTF8 type which resolves to a class signature.
    Deprecated No data stored.
    SourceDebugExtension u1 debug_extension[attribute_length];

    debug_extension
    The debug_extension array holds a string, which must be in UTF-8 format. There is no terminating zero byte.The string in the debug_extension item will be interpreted as extended debugging information. The content of this string has no semantic effect on the Java Virtual Machine.

    For example:
    If the attribute_length = 5, then 5 bytes holds the string.
    RuntimeVisible- Annotations See Table 9
    RuntimeInVisible- Annotations See Table 9


    Table 21: Nested class access specifiers
    Value (hex) Name Access specifier Description
    0x0001 ACC_PUBLIC public Marked or implicitly public in source.
    0x0002 ACC_PRIVATE private Marked private in source.
    0x0004 ACC_PROTECTED protected Marked protected in source.
    0x0008 ACC_STATIC static Marked or implicitly static in source.
    0x0010 ACC_FINAL final Marked final in source.
    0x0200 ACC_INTERFACE interface Was an interface in source.
    0x0400 ACC_ABSTRACT abstract Marked or implicitly abstract in source.
    0x1000 ACC_SYNTHETIC   Generated by the compiler and does not appear in the source code.
    0x2000 ACC_ANNOTATION   Declared as an annotation type.
    0x4000 ACC_ENUM enum Declared as an enum type.




The constant pool table can be seen as an multi dimensional array (cp_table), where the values are stored in cp_table[1] till cp_table[cp_count-1]. Please note cp_table[0] is always empty. Each constant pool table array record must always start with a Tag byte (tag byte = cp_table[n][0]), which indicates the Constant pool table type. Depending on the Tag byte you will know which constant pool table type follows and how may bytes are used for each type.

Table 3: Constant pool table types
Tag
B0
B1 B2 B3 B4 B5 B6 B7 B8 # of bytes Description
1 X1 X2 .. .. .. .. .. .. u2 + N UTF8 (CONSTANT_Utf8)

The first two bytes X1 and X2 are used to determine how many characters the UTF8 string consist of.

Each character takes up two bytes.
3 X X X X         u4 Integer (CONSTANT_Integer)

4 bytes are used to create a Integer value.
4 X X X X         u4 Float (CONSTANT_Float)

4 bytes are used to create a Float value.
5 X X X X X X X X u8 Long (CONSTANT_Long)

8 bytes are used to create a Long value.
6 X X X X X X X X u8 Double (CONSTANT_Double)

8 bytes are used to create a Double value.
7 X X             u2 Class Reference (CONSTANT_Class)

The Class Reference, refers to another record in the constant pool table. This other record is of type to UTF8.
8 X X             u2 String Reference (CONSTANT_String)

The String Reference, refers to another record in the constant pool table. This other record is of type to UTF8.
9 X1 X2 X3 X4         u4 Field Reference (CONSTANT_Fieldref)

The Field Reference, refers to 2 other records in the constant pool table.

Bytes X1 and X2 refers to a Class Reference type. When the Class Reference is resolved it will indicate a class type or interface type.

Bytes X3 and X4 refers to a Name and Type descriptor type. When the Name and Type descriptor is resolved it will indicate the field descriptor.
10 X1 X2 X3 X4         u4 Method Reference (CONSTANT_Methodref)

The Method Reference, refers to 2 other records in the constant pool table.

Bytes X1 and X2 refers to a Class Reference type. When the Class Reference is resolved it will indicate the class type.

Bytes X3 and X4 refers to a Name and Type descriptor type. When the Name and Type descriptor is resolved it will indicate the method descriptor.
11 X1 X2 X3 X4         u4 Interface Method Reference (CONSTANT_InterfaceMethodref)

The Interface Method Reference, refers to 2 other records in the constant pool table.

Bytes X1 and X2 refers to a Class Reference type. When the Class Reference is resolved it will indicate the interface type.

Bytes X3 and X4 refers to a Name and Type descriptor type. When the Name and Type descriptor is resolved it will indicate the method descriptor.
12 X1 X2 X3 X4         u4 Name and Type descriptor (CONSTANT_NameAndType)

The Name and Type descriptor, refers to 2 other records in the constant pool table.

Bytes X1 and X2 refers to a UTF8 type which resolves into a field name or method name.

Bytes X3 and X4 refers to a UTF8 type which resolves into a field type descriptor or method type descriptor.