Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

When we compile a .java file, we get a separate class file with a .class extension. The .class file consists of several sections and a constant pool is one of them.

In this quick tutorial, we’re going to explore the details of a constant pool. Also, we’ll see what are types it supports and how it formats the information.

2. Constant Pool in Java

Simply put, a constant pool contains the constants that are needed to run the code of a specific class. Basically, it’s a runtime data structure similar to the symbol table. It is a per-class or per-interface runtime representation in a Java class file.

The content of the constant pool consists of symbolic references generated by the compiler. These references are names of variables, methods, interfaces, and classes referenced from the code. The JVM uses them to link the code with other classes it depends on.

Let’s understand the structure of a constant pool using a simple Java class:

public class ConstantPool {
    
    public void sayHello() {
        System.out.println("Hello World");
    }
}

To view the constant pool’s content, we need to first compile the file and then run the command:

javap -v name.class

The above command will yield:

   #1 = Methodref          #6.#14         // java/lang/Object."<init>":()V
   #2 = Fieldref           #15.#16        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #17            // Hello World
   #4 = Methodref          #18.#19        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #20            // com/baeldung/jvm/ConstantPool
   #6 = Class              #21            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               sayHello
  #12 = Utf8               SourceFile
  #13 = Utf8               ConstantPool.java
  #14 = NameAndType        #7:#8          // "<init>":()V
  #15 = Class              #22            // java/lang/System
  #16 = NameAndType        #23:#24        // out:Ljava/io/PrintStream;
  #17 = Utf8               Hello World
  #18 = Class              #25            // java/io/PrintStream
  #19 = NameAndType        #26:#27        // println:(Ljava/lang/String;)V
  #20 = Utf8               com/baeldung/jvm/ConstantPool
  #21 = Utf8               java/lang/Object
  #22 = Utf8               java/lang/System
  #23 = Utf8               out
  #24 = Utf8               Ljava/io/PrintStream;
  #25 = Utf8               java/io/PrintStream
  #26 = Utf8               println
  #27 = Utf8               (Ljava/lang/String;)V

#n indicates the references to the constant pool. #17 is a symbolic reference to the “Hello World” String, #18 is System.out, and #19 is a println. Similarly, #8 highlights that the return type of method is void and #20 is a fully qualified class name.

It is important to note that the constant pool table starts from index 1. The index value 0 is regarded as an invalid index.

2.1. Types

The constant pool supports several types:

  • Integer, Float: with 32-bit constants
  • Double, Long: with 64-bit constants
  • String: a 16-bit string constant that points at another entry in the pool which contains the actual bytes
  • Class: contains the fully qualified class name
  • Utf8: a stream of bytes
  • NameAndType: a colon-separated pair of values, first entry represents the name while the second entry indicates the type
  • Fieldref, Methodref, InterfaceMethodref: a dot-separated pair of values, first value points at Class entry whereas the second value points as NameAndType entry

What about the other types like boolean, short, and byte? These types are represented as Integer constant in the pool.

2.2. Format

Each entry in the table obeys a general format:

cp_info {
    u1 tag;
    u1 info[];
}

The initial 1-byte tag indicates the kind of constant. Once a JVM grabs and intercepts the tags, it knows what follows the tag. Usually, the tag is followed by two or more bytes to carry the information about that constant.

Let’s look at some of the types and their tag indexes:

  • Utf8: 1
  • Integer: 3
  • Float: 4
  • Long: 5
  • Double: 6
  • Class reference: 7
  • String reference: 8

The constant pool of any class or interface is created only when the JVM has completed its loading.

3. Conclusion

In this quick article, we learned about the constant pool in JVM. We’ve seen that it contains symbolic references that are used to locate the actual objects. Also, we look at how the pool format the information about constants and their types.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW