Video in progress

Strong Middle ⚙️ JVM and memory

What are the constant pool and symbolic references in a class file, and when do they turn into real addresses?

The constant pool is a table in the class file: string literals, numbers, and class, method and field names. Bytecode calls methods not by address but by a symbolic reference (name + descriptor) in this table. HotSpot resolves them lazily: when an instruction runs for the first time it is bound to the real class/method and the result is cached.

📖 Answer

Look at it with javap -v:

Constant pool:
   #7 = Methodref     #8.#9    // com/acme/TaxService.rate:(Ljava/lang/String;)Ljava/math/BigDecimal;
   #8 = Class         #10      // com/acme/TaxService
  #14 = String        #15      // UZ
...
  12: invokevirtual #7

invokevirtual #7 means "call the method in entry number 7". There is no memory address here, only a name. That is why you can compile classes separately and later use them with a different version (and NoSuchMethodError comes from this too).

When resolution happens: the specification allows either all at once when the class is linked, or on first use. HotSpot picks the second:

void handle(boolean rare) {
    if (rare) {
        LegacyParser.parse();   // LegacyParser is looked up only when this line first runs
    }
}

If LegacyParser is missing from the jar, handle(false) keeps working without trouble, while handle(true) throws NoClassDefFoundError. This is one cause of the "it worked for a year and then suddenly crashed" cases on prod: a rarely executed branch.

A resolved entry is cached (the constant pool cache), and later executions don't look it up. The JIT uses what was already resolved at compile time directly, and if it reaches something unresolved, it puts in an "uncommon trap".

The practical upshot: to catch classpath problems even in places with little test coverage, you need jdeps or integration tests at startup. The JVM won't check them ahead of time.