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, and everything
after it refers to its entries by a two-byte index. The explorer shows the pool as a
table, with each entry resolved to readable text.
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, 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.
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 unusable, and no entry is stored for it.
- 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.
| 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)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 3 |
| 1 | 4 | i4 | bytes | A 32-bit signed integer, big-endian |
Float (4)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 4 |
| 1 | 4 | f4 | bytes | An IEEE 754 binary32 value, big-endian |
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 |
A 64-bit two’s complement number that takes two slots.
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 |
An IEEE 754 binary64 value, big-endian, that takes two slots.
Class (7)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 7 |
| 1 | 2 | u2 | name_index | A Utf8 entry: the class name with / between packages, such as java/lang/String, or for an array type its descriptor |
String (8)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 8 |
| 1 | 2 | u2 | string_index | A Utf8 entry: the characters of a String constant |
Fieldref (9)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 9 |
| 1 | 2 | u2 | class_index | A Class entry: the class the field belongs to |
| 3 | 2 | u2 | name_and_type_index | A NameAndType entry with a field descriptor |
Methodref (10)
| 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 with a method descriptor |
InterfaceMethodref (11)
| 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 with a method descriptor; used by invokeinterface |
NameAndType (12)
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 1 | u1 | tag | 12 |
| 1 | 2 | u2 | name_index | A Utf8 entry: a field or method name, or <init> |
| 3 | 2 | u2 | descriptor_index | A Utf8 entry: a field or method descriptor |
Modified UTF-8
Utf8 entries use the encoding Java’s DataInput.readUTF reads, which
differs from standard UTF-8 in that no byte is ever zero:
- U+0001 to U+007F: one byte,
0xxxxxxx. - U+0000, and U+0080 to U+07FF: two bytes,
110xxxxx 10xxxxxx. NUL isC0 80. - U+0800 to U+FFFF: three bytes,
1110xxxx 10xxxxxx 10xxxxxx. - No byte may be
00, orF0toFF.
Java’s characters were 16-bit at the time, so nothing beyond U+FFFF arises. The explorer decodes each entry and marks one whose bytes break these rules “Not valid modified UTF-8”.
Tags of later formats
The class file formats that followed added six tags. A file of this format has none of them; the explorer turns down a file that does.
| Tag | Name |
|---|---|
| 15 | MethodHandle |
| 16 | MethodType |
| 17 | Dynamic |
| 18 | InvokeDynamic |
| 19 | Module |
| 20 | Package |
The reference kinds of a MethodHandle entry, and the bootstrap method table that Dynamic and InvokeDynamic entries point into, belong to those later formats too.
An example pool
The start of the pool of the example file, from offset 8:
00000000 CA FE BA BE 00 02 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 | Value |
|---|---|---|---|
| #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 | ()V |
| #6 | 01 00 06 3C 69 6E 69 74 3E | Utf8 | <init> |
| #7 | 01 00 04 43 6F 64 65 | Utf8 | Code |
Entries #8 to #14 are the Utf8 entries ConstantValue,
EmptyStackException.java, Exceptions,
LocalVariables, SourceFile,
java/lang/RuntimeException and
java/util/EmptyStackException.