6 JVM Constants API
The JVM Constants API is defined in the package
java.lang.constants
, which contains the nominal descriptors of various
types of loadable constants. These nominal descriptors are useful for applications that
manipulate class files and compile-time or link-time program analysis tools.
A nominal descriptor is not the value of a loadable constant but a description of its
value, which can be reconstituted given a class loading context. A loadable constant is
a constant pool entry that can be pushed onto the operand stack or can appear in the
static argument list of a bootstrap method for the invokedynamic
instruction. The operand stack is where JVM instructions get their input and store their
output. Every Java class file has a constant pool, which contains several kinds of
constants, ranging from numeric literals known at compile-time to method and field
references that must be resolved at run-time.
The issue with working with non-nominal loadable constants, such as a
Class
objects, whose references are resolved at run-time, is that
these references depend on the correctness and consistency of the class loading context.
Class loading may have side effects, such as running code that you don't want run and
throwing access-related and out-of-memory exceptions, which you can avoid with nominal
descriptions. In addition, class loading may not be possible at all.
See the package java.lang.constant
.