The Constant Pool
Java Virtual Machine instructions do not rely on the run-time layout of classes, interfaces, class instances, or arrays. Instead, instructions refer to symbolic information in the constant_pool
table.
All constant_pool
table entries have the following general format:
cp_info {
u1 tag;
u1 info[];
}
Each item in the constant_pool
table must begin with a 1-byte tag indicating the kind of cp_info
entry. The contents of the info
array vary with the value of tag
. The valid tags and their values are listed in Table 4.4-A. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.
Table 4.4-A. Constant pool tags
Constant Type | Value |
| 7 |
| 9 |
| 10 |
| 11 |
| 8 |
| 3 |
| 4 |
| 5 |
| 6 |
| 12 |
| 1 |
| 15 |
| 16 |
| 18 |
4.4.1. The CONSTANT_Class_info
Structure
The CONSTANT_Class_info
structure is used to represent a class or an interface:
CONSTANT_Class_info {
u1 tag;
u2 name_index;
}
The items of the CONSTANT_Class_info
structure are as follows:
tag
The tag
item has the value CONSTANT_Class
(7).
name_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 be a CONSTANT_Utf8_info
structure (§4.4.7) representing a valid binary class or interface name encoded in internal form (§4.2.1).
Because arrays are objects, the opcodes anewarray and multianewarray - but not the opcode new - can reference array "classes" via CONSTANT_Class_info
structures in the constant_pool
table. For such array classes, the name of the class is the descriptor of the array type (§4.3.2).
For example, the class name representing the two-dimensional array type int[][]
is [[I
, while the class name representing the type Thread[]
is [Ljava/lang/Thread;
.
An array type descriptor is valid only if it represents 255 or fewer dimensions.
4.4.2. The CONSTANT_Fieldref_info
, CONSTANT_Methodref_info
, and CONSTANT_InterfaceMethodref_info
Structures
Fields, methods, and interface methods are represented by similar structures:
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_InterfaceMethodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
The items of these structures are as follows:
tag
The tag
item of a CONSTANT_Fieldref_info
structure has the value CONSTANT_Fieldref
(9).
The tag
item of a CONSTANT_Methodref_info
structure has the value CONSTANT_Methodref
(10).
The tag
item of a CONSTANT_InterfaceMethodref_info
structure has the value CONSTANT_InterfaceMethodref
(11).
class_index
The value of the class_index
item must be a valid index into the constant_pool
table. The constant_pool
entry at that index must be a CONSTANT_Class_info
structure (§4.4.1) representing a class or interface type that has the field or method as a member.
The class_index
item of a CONSTANT_Methodref_info
structure must be a class type, not an interface type.
The class_index
item of a CONSTANT_InterfaceMethodref_info
structure must be an interface type.
The class_index
item of a CONSTANT_Fieldref_info
structure may be either a class type or an interface type.
name_and_type_index
The value of the name_and_type_index
item must be a valid index into the constant_pool
table. The constant_pool
entry at that index must be a CONSTANT_NameAndType_info
structure (§4.4.6). This constant_pool
entry indicates the name and descriptor of the field or method.
In a CONSTANT_Fieldref_info
, the indicated descriptor must be a field descriptor (§4.3.2). Otherwise, the indicated descriptor must be a method descriptor (§4.3.3).
If the name of the method of a CONSTANT_Methodref_info
structure begins with a '<
' ('\u003c
'), then the name must be the special name <init>
, representing an instance initialization method (§2.9). The return type of such a method must be void
.
4.4.3. The CONSTANT_String_info
Structure
The CONSTANT_String_info
structure is used to represent constant objects of the type String
:
CONSTANT_String_info {
u1 tag;
u2 string_index;
}
The items of the CONSTANT_String_info
structure are as follows:
tag
The tag
item of the CONSTANT_String_info
structure has the value CONSTANT_String
(8).
string_index
The value of the string_index
item must be a valid index into the constant_pool
table. The constant_pool
entry at that index must be a CONSTANT_Utf8_info
structure (§4.4.7) representing the sequence of Unicode code points to which the String
object is to be initialized.
4.4.4. The CONSTANT_Integer_info
and CONSTANT_Float_info
Structures
The CONSTANT_Integer_info
and CONSTANT_Float_info
structures represent 4-byte numeric (int
and float
) constants:
CONSTANT_Integer_info {
u1 tag;
u4 bytes;
}
CONSTANT_Float_info {
u1 tag;
u4 bytes;
}
The items of these structures are as follows:
tag
The tag
item of the CONSTANT_Integer_info
structure has the value CONSTANT_Integer
(3).
The tag
item of the CONSTANT_Float_info
structure has the value CONSTANT_Float
(4).
bytes
The bytes
item of the CONSTANT_Integer_info
structure represents the value of the int
constant. The bytes of the value are stored in big-endian (high byte first) order.
The bytes
item of the CONSTANT_Float_info
structure represents the value of the float
constant in IEEE 754 floating-point single format (§2.3.2). The bytes of the single format representation are stored in big-endian (high byte first) order.
The value represented by the CONSTANT_Float_info
structure is determined as follows. The bytes of the value are first converted into an int
constant bits. Then:
- If bits is
0x7f800000
, the float
value will be positive infinity. - If bits is
0xff800000
, the float
value will be negative infinity. - If bits is in the range
0x7f800001
through 0x7fffffff
or in the range 0xff800001
through 0xffffffff
, the float
value will be NaN. - In all other cases, let
s
, e
, and m
be three values that might be computed from bits:
int s = ((bits >> 31) == 0) ? 1 : -1; int e = ((bits >> 23) & 0xff); int m = (e == 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;
Then the float
value equals the result of the mathematical expression s · m · 2e-150
.
4.4.5. The CONSTANT_Long_info
and CONSTANT_Double_info
Structures
The CONSTANT_Long_info
and CONSTANT_Double_info
represent 8-byte numeric (long
and double
) constants:
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
All 8-byte constants take up two entries in the constant_pool
table of the class
file. If a CONSTANT_Long_info
or CONSTANT_Double_info
structure is the item in the constant_pool
table at index n, then the next usable item in the pool is located at index n+2. The constant_pool
index n+1 must be valid but is considered unusable.
In retrospect, making 8-byte constants take two constant pool entries was a poor choice.
The items of these structures are as follows:
tag
The tag
item of the CONSTANT_Long_info
structure has the value CONSTANT_Long
(5).
The tag
item of the CONSTANT_Double_info
structure has the value CONSTANT_Double
(6).
high_bytes, low_bytes
The unsigned high_bytes
and low_bytes
items of the CONSTANT_Long_info
structure together represent the value of the long
constant
((long) high_bytes << 32) + low_bytes
where the bytes of each of high_bytes
and low_bytes
are stored in big-endian (high byte first) order.
The high_bytes
and low_bytes
items of the CONSTANT_Double_info
structure together represent the double
value in IEEE 754 floating-point double format (§2.3.2). The bytes of each item are stored in big-endian (high byte first) order.
The value represented by the CONSTANT_Double_info
structure is determined as follows. The high_bytes
and low_bytes
items are converted into the long
constant bits, which is equal to
((long) high_bytes << 32) + low_bytes
Then:
- If bits is
0x7ff0000000000000L
, the double
value will be positive infinity. - If bits is
0xfff0000000000000L
, the double
value will be negative infinity. - If bits is in the range
0x7ff0000000000001L
through 0x7fffffffffffffffL
or in the range 0xfff0000000000001L
through 0xffffffffffffffffL
, the double value will be NaN. - In all other cases, let
s
, e
, and m
be three values that might be computed from bits:
int s = ((bits >> 63) == 0) ? 1 : -1; int e = (int)((bits >> 52) & 0x7ffL); long m = (e == 0) ? (bits & 0xfffffffffffffL) << 1 : (bits & 0xfffffffffffffL) | 0x10000000000000L;
Then the floating-point value equals the double
value of the mathematical expression s · m · 2e-1075
.
4.4.6. The CONSTANT_NameAndType_info
Structure
The CONSTANT_NameAndType_info
structure is used to represent a field or method, without indicating which class or interface type it belongs to:
CONSTANT_NameAndType_info {
u1 tag;
u2 name_index;
u2 descriptor_index;
}
The items of the CONSTANT_NameAndType_info
structure are as follows:
tag
The tag
item of the CONSTANT_NameAndType_info
structure has the value CONSTANT_NameAndType
(12).
name_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 be a CONSTANT_Utf8_info
structure (§4.4.7) representing either the special method name <init>
(§2.9) or a valid unqualified name denoting a field or method (§4.2.2).
descriptor_index
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 be a CONSTANT_Utf8_info
structure (§4.4.7) representing a valid field descriptor or method descriptor (§4.3.2, §4.3.3).
4.4.7. The CONSTANT_Utf8_info
Structure
The CONSTANT_Utf8_info
structure is used to represent constant string values:
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
The items of the CONSTANT_Utf8_info
structure are as follows:
tag
The tag
item of the CONSTANT_Utf8_info
structure has the value CONSTANT_Utf8
(1).
length
The value of the length
item gives the number of bytes in the bytes
array (not the length of the resulting string).
bytes[]
The bytes
array contains the bytes of the string.
No byte may have the value (byte)0
.
No byte may lie in the range (byte)0xf0
to (byte)0xff
.
String content is encoded in modified UTF-8. Modified UTF-8 strings are encoded so that code point sequences that contain only non-null ASCII characters can be represented using only 1 byte per code point, but all code points in the Unicode codespace can be represented. Modified UTF-8 strings are not null-terminated. The encoding is as follows:
- Code points in the range '
\u0001
' to '\u007F
' are represented by a single byte:
Table 4.4.
The 7 bits of data in the byte give the value of the code point represented.
The null code point ('
\u0000
') and code points in the range '\u0080
' to '\u07FF
' are represented by a pair of bytes x
and y
:Table 4.5.
x
:Table 4.6.
110bits 10-6
y
:Table 4.7.
10bits 5-0The two bytes represent the code point with the value:
((x & 0x1f) << 6) + (y & 0x3f)
Code points in the range '
\u0800
' to '\uFFFF
' are represented by 3 bytes x
, y
, and z
:Table 4.8.
x
:Table 4.9.
1110bits 15-12
y
:Table 4.10.
10bits 11-6
z
:Table 4.11.
10bits 5-0The three bytes represent the code point with the value:
((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f)
Characters with code points above U+FFFF (so-called supplementary characters) are represented by separately encoding the two surrogate code units of their UTF-16 representation. Each of the surrogate code units is represented by three bytes. This means supplementary characters are represented by six bytes,
u
, v
, w
, x
, y
, and z
:Table 4.12.
u
:Table 4.13.
11101101
v
:Table 4.14.
1010(bits 20-16)-1
w
:Table 4.15.
10bits 15-10
x
:Table 4.16.
11101101
y
:Table 4.17.
1011bits 9-6
z
:Table 4.18.
10bits 5-0The six bytes represent the code point with the value:
0x10000 + ((v & 0x0f) << 16) + ((w & 0x3f) << 10) +((y & 0x0f) << 6) + (z & 0x3f)
The bytes of multibyte characters are stored in the class
file in big-endian (high byte first) order.
There are two differences between this format and the "standard" UTF-8 format. First, the null character (char)0
is encoded using the 2-byte format rather than the 1-byte format, so that modified UTF-8 strings never have embedded nulls. Second, only the 1-byte, 2-byte, and 3-byte formats of standard UTF-8 are used. The Java Virtual Machine does not recognize the four-byte format of standard UTF-8; it uses its own two-times-three-byte format instead.
For more information regarding the standard UTF-8 format, see Section 3.9 Unicode Encoding Forms of The Unicode Standard, Version 6.0.0.
4.4.8. The CONSTANT_MethodHandle_info
Structure
The CONSTANT_MethodHandle_info
structure is used to represent a method handle:
CONSTANT_MethodHandle_info {
u1 tag;
u1 reference_kind;
u2 reference_index;
}
The items of the CONSTANT_MethodHandle_info
structure are the following:
tag
The tag
item of the CONSTANT_MethodHandle_info
structure has the value CONSTANT_MethodHandle
(15).
reference_kind
The value of the reference_kind
item must be in the range 1 to 9. The value denotes the kind of this method handle, which characterizes its bytecode behavior (§5.4.3.5).
reference_index
The value of the reference_index
item must be a valid index into the constant_pool
table. The constant_pool
entry at that index must be as follows:
- If the value of the
reference_kind
item is 1 (REF_getField
), 2 (REF_getStatic
), 3 (REF_putField
), or 4 (REF_putStatic
), then the constant_pool
entry at that index must be a CONSTANT_Fieldref_info
(§4.4.2) structure representing a field for which a method handle is to be created. - If the value of the
reference_kind
item is 5 (REF_invokeVirtual
) or 8 (REF_newInvokeSpecial
), then the constant_pool
entry at that index must be a CONSTANT_Methodref_info
structure (§4.4.2) representing a class's method or constructor (§2.9) for which a method handle is to be created. - If the value of the
reference_kind
item is 6 (REF_invokeStatic
) or 7 (REF_invokeSpecial
), then if the class
file version number is less than 52.0, the constant_pool
entry at that index must be a CONSTANT_Methodref_info
structure representing a class's method for which a method handle is to be created; if the class
file version number is 52.0 or above, the constant_pool
entry at that index must be either a CONSTANT_Methodref_info
structure or a CONSTANT_InterfaceMethodref_info
structure (§4.4.2) representing a class's or interface's method for which a method handle is to be created. - If the value of the
reference_kind
item is 9 (REF_invokeInterface
), then the constant_pool
entry at that index must be a CONSTANT_InterfaceMethodref_info
structure representing an interface's method for which a method handle is to be created.
If the value of the reference_kind
item is 5 (REF_invokeVirtual
), 6 (REF_invokeStatic
), 7 (REF_invokeSpecial
), or 9 (REF_invokeInterface
), the name of the method represented by a CONSTANT_Methodref_info
structure or a CONSTANT_InterfaceMethodref_info
structure must not be <init>
or <clinit>
.
If the value is 8 (REF_newInvokeSpecial
), the name of the method represented by a CONSTANT_Methodref_info
structure must be <init>
.
4.4.9. The CONSTANT_MethodType_info
Structure
The CONSTANT_MethodType_info
structure is used to represent a method type:
CONSTANT_MethodType_info {
u1 tag;
u2 descriptor_index;
}
The items of the CONSTANT_MethodType_info
structure are as follows:
tag
The tag
item of the CONSTANT_MethodType_info
structure has the value CONSTANT_MethodType
(16).
descriptor_index
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 be a CONSTANT_Utf8_info
structure (§4.4.7) representing a method descriptor (§4.3.3).
4.4.10. The CONSTANT_InvokeDynamic_info
Structure
The CONSTANT_InvokeDynamic_info
structure is used by an invokedynamic instruction (§invokedynamic) to specify a bootstrap method, the dynamic invocation name, the argument and return types of the call, and optionally, a sequence of additional constants called static arguments to the bootstrap method.
CONSTANT_InvokeDynamic_info {
u1 tag;
u2 bootstrap_method_attr_index;
u2 name_and_type_index;
}
The items of the CONSTANT_InvokeDynamic_info
structure are as follows:
tag
The tag
item of the CONSTANT_InvokeDynamic_info
structure has the value CONSTANT_InvokeDynamic
(18).
bootstrap_method_attr_index
The value of the bootstrap_method_attr_index
item must be a valid index into the bootstrap_methods
array of the bootstrap method table (§4.7.23) of this class
file.
name_and_type_index
The value of the name_and_type_index
item must be a valid index into the constant_pool
table. The constant_pool
entry at that index must be a CONSTANT_NameAndType_info
structure (§4.4.6) representing a method name and method descriptor (§4.3.3).
4.5. Fields
Each field is described by a field_info
structure.
No two fields in one class
file may have the same name and descriptor (§4.3.2).
The structure has the following format:
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
The items of the field_info
structure are as follows:
access_flags
The value of the access_flags
item is a mask of flags used to denote access permission to and properties of this field. The interpretation of each flag, when set, is specified in Table 4.5-A.
Table 4.5-A. Field access and property flags
Flag Name | Value | Interpretation |
| 0x0001 | Declared |
| 0x0002 | Declared |
| 0x0004 | Declared |
| 0x0008 | Declared |
| 0x0010 | Declared |
| 0x0040 | Declared |
| 0x0080 | Declared |
| 0x1000 | Declared synthetic; not present in the source code. |
| 0x4000 | Declared as an element of an |
Fields of classes may set any of the flags in Table 4.5-A. However, each field of a class may have at most one of its ACC_PUBLIC
, ACC_PRIVATE
, and ACC_PROTECTED
flags set (JLS §8.3.1), and must not have both its ACC_FINAL
and ACC_VOLATILE
flags set (JLS §8.3.1.4).
Fields of interfaces must have their ACC_PUBLIC
, ACC_STATIC
, and ACC_FINAL
flags set; they may have their ACC_SYNTHETIC
flag set and must not have any of the other flags in Table 4.5-A set (JLS §9.3).
The ACC_SYNTHETIC
flag indicates that this field was generated by a compiler and does not appear in source code.
The ACC_ENUM
flag indicates that this field is used to hold an element of an enumerated type.
All bits of the access_flags
item not assigned in Table 4.5-A are reserved for future use. They should be set to zero in generated class
files and should be ignored by Java Virtual Machine implementations.
name_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 be a CONSTANT_Utf8_info
structure (§4.4.7) which represents a valid unqualified name denoting a field (§4.2.2).
descriptor_index
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 be a CONSTANT_Utf8_info
structure (§4.4.7) which represents a valid field descriptor (§4.3.2).
attributes_count
The value of the attributes_count
item indicates the number of additional attributes of this field.
attributes[]
Each value of the attributes
table must be an attribute_info
structure (§4.7).
A field can have any number of optional attributes associated with it.
The attributes defined by this specification as appearing in the attributes
table of a field_info
structure are listed in Table 4.7-C.
The rules concerning attributes defined to appear in the attributes
table of a field_info
structure are given in §4.7.
The rules concerning non-predefined attributes in the attributes
table of a field_info
structure are given in §4.7.1.