ConstantValue attribute
The value of a static final field of primitive or String type whose initialiser is a compile-time constant.
- Appears on
- field
- Since
- class file 45.0 (Java 1.0)
- javac writes it
- for static final fields with a compile-time constant value
- Needed by the JVM
- yes: the JVM reads it to load, verify or run the class
Layout
| Type | Field | What it holds |
|---|---|---|
u2 | attribute_name_index | a Utf8: "ConstantValue" |
u4 | attribute_length | bytes after this field |
u2 | constantvalue_index | an Integer, Long, Float, Double or String constant |
What it is for
The value of a static final field of primitive or String type whose initialiser is a compile-time constant. The JVM sets the field when the class is prepared, before the static initialiser runs, so there is no putstatic for it.
javac also copies the value into every class that reads the field. Changing the constant and recompiling only its own class leaves the old value in the others.
How common it is
JDK 25.0.5's java.base has 5,773 ConstantValue attributes, in 1,447 of its 7,391 classes (5,773 on fields).
A real one, byte by byte
From Constants.class (compiled by javac 25), on field LIMIT:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 571 | 8 bytes | ConstantValue | |
| 571 | 00 24 | attribute_name_index | #36 |
| 573 | 00 00 00 02 | attribute_length | 2 |
| 577 | 00 25 | constantvalue_index | #37 |
Specification: JVMS §4.7, The ConstantValue Attribute (Java SE 25).