The constant pool is a numbered table of every name, string, number and reference the
class uses. It follows constant_pool_count at offset 8. Everything later in
the file, from this_class to the operands of the bytecode, refers to its
entries by a two-byte index. The explorer shows the pool as a table, with each entry
resolved to the text javap would give.
Entries and indexes
Each entry is a one-byte tag and a body whose layout the tag decides. Every body has a fixed size except Utf8’s, which gives its length in its first two bytes. There is no table of offsets, so a reader must step through every entry to find the end of the pool.
- Indexes start at 1. Index 0 is never an entry; where a field allows 0 it means “none”.
constant_pool_countis one more than the number of slots, so the valid indexes are 1 to count − 1.- A Long or Double entry takes two slots. The index after it is valid but unusable: nothing may refer to it and no entry is stored for it. The specification itself says: “In retrospect, making 8-byte constants take two constant pool entries was a poor choice.”
- Entries may refer to entries later in the pool, and the pool may hold entries nothing uses.
Utf8 (1)
A string: a name, a descriptor, an attribute name or the characters of a string literal. It is the only entry of variable size.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 1 |
| 1 | 2 | u2 | length | The number of bytes that follow (not characters) |
| 3 | length | u1[] | bytes | The text in modified UTF-8, with no terminator |
01 00 06 3C 69 6E 69 74 3E is the Utf8 entry <init>.
Integer (3)
A 32-bit signed integer constant, for int values outside the range
sipush can push, and for int, short,
char, byte and boolean constant fields.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 3 |
| 1 | 4 | i4 | bytes | The value, big-endian two’s complement |
Float (4)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 4 |
| 1 | 4 | f4 | bytes | An IEEE 754 binary32 value, big-endian |
04 3F C0 00 00 is 1.5f. Infinities and NaN are stored as their IEEE
bit patterns.
Long (5)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 5 |
| 1 | 4 | u4 | high_bytes | The high 32 bits |
| 5 | 4 | u4 | low_bytes | The low 32 bits |
The value is (high_bytes << 32) + low_bytes, a 64-bit two’s
complement number. The entry takes two slots: if it is #7, the next entry is #9.
05 FF FF FF FF FF FF FF FE is −2.
Double (6)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 6 |
| 1 | 4 | u4 | high_bytes | The high 32 bits |
| 5 | 4 | u4 | low_bytes | The low 32 bits |
The eight bytes are an IEEE 754 binary64 value, big-endian. Like Long, it takes two
slots. 06 40 09 21 FB 54 44 2D 18 is π, 3.141592653589793.
Class (7)
A class or interface, named by this_class, super_class,
the interfaces, member references and instructions such as new and
checkcast.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 7 |
| 1 | 2 | u2 | name_index | A Utf8 entry: the name in internal form, such as java/lang/String, or for an array type its descriptor, such as [I |
String (8)
A java.lang.String constant, as loaded by ldc for a string
literal. Equal literals in all classes become the same object.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 8 |
| 1 | 2 | u2 | string_index | A Utf8 entry: the characters |
Fieldref (9)
A field of some class, as used by getfield, putfield,
getstatic and putstatic.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 9 |
| 1 | 2 | u2 | class_index | A Class entry: the class or interface the field is looked up in |
| 3 | 2 | u2 | name_and_type_index | A NameAndType entry whose descriptor is a field descriptor |
Methodref (10)
A method of a class (not an interface), as used by invokevirtual,
invokespecial and invokestatic.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 10 |
| 1 | 2 | u2 | class_index | A Class entry that names a class |
| 3 | 2 | u2 | name_and_type_index | A NameAndType entry whose descriptor is a method descriptor; a name beginning < must be <init> with a V return |
0A 00 01 00 04: a Methodref with class #1 and name and type #4.
InterfaceMethodref (11)
A method of an interface, as used by invokeinterface and, from 52.0,
by invokestatic and invokespecial for static and private
interface methods.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 11 |
| 1 | 2 | u2 | class_index | A Class entry that names an interface |
| 3 | 2 | u2 | name_and_type_index | A NameAndType entry whose descriptor is a method descriptor |
NameAndType (12)
A name and a descriptor together, without saying which class they belong to. The three member references, Dynamic and InvokeDynamic point to one.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 12 |
| 1 | 2 | u2 | name_index | A Utf8 entry: an unqualified name, or <init> |
| 3 | 2 | u2 | descriptor_index | A Utf8 entry: a field or method descriptor |
0C 00 06 00 05: name #6 (<init>) and descriptor #5
(()V).
MethodHandle (15)
A java.lang.invoke.MethodHandle constant: a direct reference to a field
access, a method call or a constructor. Used as bootstrap methods and their arguments,
and by ldc. From 51.0.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 15 |
| 1 | 1 | u1 | reference_kind | 1 to 9: what the handle does (see reference kinds) |
| 2 | 2 | u2 | reference_index | The Fieldref, Methodref or InterfaceMethodref the handle refers to |
MethodType (16)
A java.lang.invoke.MethodType constant: a method’s parameter and return
types. From 51.0.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 16 |
| 1 | 2 | u2 | descriptor_index | A Utf8 entry: a method descriptor |
Dynamic (17)
A constant whose value a bootstrap method computes the first time it is used. From 55.0.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 17 |
| 1 | 2 | u2 | bootstrap_method_attr_index | An index into the BootstrapMethods table (not into the pool) |
| 3 | 2 | u2 | name_and_type_index | A NameAndType entry whose descriptor is a field descriptor: the constant’s type |
InvokeDynamic (18)
A call site for the invokedynamic instruction, which javac uses for
lambdas, string concatenation from Java 9, records’ methods and pattern-matching
switches. From 51.0.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 18 |
| 1 | 2 | u2 | bootstrap_method_attr_index | An index into the BootstrapMethods table |
| 3 | 2 | u2 | name_and_type_index | A NameAndType entry whose descriptor is a method descriptor: the call site’s type |
The source side of lambdas is described on the Java lambdas page.
Module (19)
A module, named in a module-info file’s Module attribute. It may appear only in a class file with ACC_MODULE set. From 53.0.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 19 |
| 1 | 2 | u2 | name_index | A Utf8 entry: the module name, with dots, such as java.base |
Package (20)
A package that a module exports or opens. Only in module-info files. From 53.0.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 20 |
| 1 | 2 | u2 | name_index | A Utf8 entry: the package name in internal form, such as java/util |
Reference kinds
A MethodHandle’s reference_kind says what the handle does, and so what
its reference_index must point to. The names are those of the constants
in java.lang.invoke.MethodHandleInfo.
| Kind | Name | Does | reference_index |
|---|---|---|---|
| 1 | REF_getField | reads an instance field | Fieldref |
| 2 | REF_getStatic | reads a static field | Fieldref |
| 3 | REF_putField | writes an instance field | Fieldref |
| 4 | REF_putStatic | writes a static field | Fieldref |
| 5 | REF_invokeVirtual | calls an instance method | Methodref |
| 6 | REF_invokeStatic | calls a static method | Methodref, or InterfaceMethodref from 52.0 |
| 7 | REF_invokeSpecial | calls a method without virtual dispatch | Methodref, or InterfaceMethodref from 52.0 |
| 8 | REF_newInvokeSpecial | makes a new object and runs a constructor | Methodref named <init> |
| 9 | REF_invokeInterface | calls an interface method | InterfaceMethodref |
Kinds 5 to 7 and 9 may not name <init> or
<clinit>; kind 8 must name <init>.
Modified UTF-8
Utf8 entries do not use standard UTF-8. They use the same encoding as
java.io.DataInput.readUTF, which differs in two ways: the zero character
is written with two bytes, so no byte is ever zero, and characters beyond U+FFFF are
written as two surrogates of three bytes each. Each character of the string is encoded
like this:
- U+0001 to U+007F: one byte,
0xxxxxxx. - U+0000, and U+0080 to U+07FF: two bytes,
110xxxxx 10xxxxxx. So NUL isC0 80, never00. - U+0800 to U+FFFF: three bytes,
1110xxxx 10xxxxxx 10xxxxxx. - A character above U+FFFF is first split into its UTF-16 surrogate pair, and each surrogate is written as three bytes by step 3: six bytes in all. The four-byte form of standard UTF-8 is never used.
- No byte may be
00, orF0toFF.
For example, é (U+00E9) is C3 A9, as in standard UTF-8,
but the emoji 😀 (U+1F600, surrogates D83D and DE00) is ED A0 BD ED B8 80
instead of standard UTF-8’s F0 9F 98 80.
The explorer decodes each entry to a string. Where the bytes do not follow these rules it puts U+FFFD in place of the bad bytes and marks the entry “Not valid modified UTF-8”.
An example pool
The start of the pool of java/util/EmptyStackException.class (JDK 1.1.4),
from offset 8:
00000000 CA FE BA BE 00 03 00 2D 00 0F 07 00 0D 07 00 0E
00000010 0A 00 01 00 04 0C 00 06 00 05 01 00 03 28 29 56
00000020 01 00 06 3C 69 6E 69 74 3E 01 00 04 43 6F 64 65
| Index | Bytes | Entry | javap shows |
|---|---|---|---|
| #1 | 07 00 0D | Class, name #13 | java/lang/RuntimeException |
| #2 | 07 00 0E | Class, name #14 | java/util/EmptyStackException |
| #3 | 0A 00 01 00 04 | Methodref, #1 and #4 | java/lang/RuntimeException."<init>":()V |
| #4 | 0C 00 06 00 05 | NameAndType, #6 and #5 | "<init>":()V |
| #5 | 01 00 03 28 29 56 | Utf8, 3 bytes | ()V |
| #6 | 01 00 06 3C 69 6E 69 74 3E | Utf8, 6 bytes | <init> |
| #7 | 01 00 04 43 6F 64 65 | Utf8, 4 bytes | Code |
Entries #1 to #4 refer forward to Utf8 entries later in the pool. The rest of this file is taken apart on the ClassFile structure page.