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

TypeFieldWhat it holds
u2attribute_name_indexa Utf8: "ConstantValue"
u4attribute_lengthbytes after this field
u2constantvalue_indexan 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:

OffsetBytesFieldValue
5718 bytesConstantValue
57100 24attribute_name_index#36
57300 00 00 02attribute_length2
57700 25constantvalue_index#37

Open in the viewer

Specification: JVMS §4.7, The ConstantValue Attribute (Java SE 25).