What Is the Java Class Constant Pool and How Does It Work at Runtime?
This article explains the structure, contents, and characteristics of Java's Class constant pool and runtime constant pool, detailing how literals and symbolic references are stored, how the runtime pool can be dynamically modified, and the impact on memory and JVM linking.
Definition
Class Constant Pool
Class constant pool is part of the .class file; after compilation it stores literals and symbolic references generated at compile time.
String literals, primitive constants, fully‑qualified class and interface names, field and method names, etc., are stored here. Its content is fixed during compilation.
The pool resides in the class file and is read into memory when the class is loaded.
Runtime Constant Pool
The runtime constant pool is a part of the JVM method area. When a class is loaded, its class constant pool is copied into the runtime constant pool, which can also receive new constants dynamically, e.g., via String.intern().
It exists in the JVM method area, created with class loading and destroyed with class unloading.
Content and Characteristics
Class Constant Pool
Content : mainly stores literals and symbolic references. Literals include text strings and final constant values; symbolic references include fully‑qualified class names, field names and descriptors, method names and descriptors, used for compile‑time resolution and verification.
public class ConstantPoolTest {
// basic type constant
private static final int CONSTANT_INT = 10;
// string constant
private static final String CONSTANT_STRING = "xuanwu";
public void print() {
System.out.println(CONSTANT_STRING);
}
}After compilation, the class constant pool contains the integer constant CONSTANT_INT with value 10, the string constant CONSTANT_STRING with value "xuanwu", and a symbolic reference to the method System.out.println.
We can view it with the jclasslib plugin:
Characteristics : The content of the class constant pool is determined at compile time and does not change at runtime. It is static information unique to each .class file.
Runtime Constant Pool
Content : contains the constants loaded from the class constant pool and can be dynamically extended.
String s1 = "java";
String s2 = new String("java").intern();The literal "java" is placed in the class constant pool at compile time and moves to the runtime constant pool after class loading. The intern() call attempts to add the newly created string object to the runtime pool; if an identical string already exists, the existing reference is returned, ensuring only one copy of "java" in the runtime pool.
Characteristics : The runtime constant pool is dynamic; multiple classes may share constants. Because it is tied to JVM memory management, it can trigger OutOfMemoryError: PermGen space (JDK 1.7 and earlier) or OutOfMemoryError: Metaspace (JDK 1.8+).
Purpose
Class Constant Pool
Provides necessary information for class loading and linking. During the resolution phase, the JVM uses symbolic references in the class constant pool to locate actual memory addresses of classes, fields, and methods, ensuring compile‑time consistency.
Runtime Constant Pool
Offers storage and access for constants at runtime, supports Java’s string constant pool mechanism, saves memory by sharing identical strings, and serves as the basis for dynamic linking by resolving symbolic references to direct references.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Xuanwu Backend Tech Stack
Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
