Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Inside the JVM

    Class Loading

    The stage crew doesn't bring every prop in before the show — only when a scene needs it. Each prop is fetched, inspected for safety, set up, and

    Chapter 2 of 104 min read

    The Plain Picture#

    The stage crew doesn't bring every prop in before the show — only when a scene needs it. Each prop is fetched, inspected for safety, set up, and then used. And there's a chain of command: a junior crew member asks their supervisor first, "do we already have this prop from the official store?"

    The Real Names#

    Classes are loaded lazily — the first time they're actually used — in three phases:

    1. Loading — find the .class bytes (in a .jar file, a folder, or the JDK itself) and create the class's internal representation in metaspace (Ch 3).

    2. Linking:

      • verification — check the bytecode is valid and safe (no jumping into the middle of an instruction, no using an int as a reference);

      • preparation — allocate static fields and set them to default values (0, null);

      • resolution — turn symbolic references ("the method score in class Quiz") into direct references (often done lazily later).

    3. Initialisation — run the class's static initialisers and static field assignments, once, in order (Object-Oriented Programming Ch 9). It happens on first active use: creating an instance, calling a static method, reading a non-constant static field.

    Class loaders form a hierarchy:

      Bootstrap class loader      core JDK classes (java.lang.String, java.util.*) — built into the JVM
              ▲
      Platform class loader       other JDK modules
              ▲
      Application class loader    YOUR classes and libraries (the "classpath")
              ▲
      custom loaders              frameworks, app servers, plugins

    Parent delegation: asked to load a class, a loader first asks its parent; only if the parent can't find it does it load the class itself. So the bootstrap loader always supplies java.lang.String — nobody can sneak in a fake String class by putting one on the classpath.

    Under the Hood — two confusingly named errors#

    ClassNotFoundExceptionNoClassDefFoundError
    Kindchecked exceptionerror (LinkageError)
    Whencode explicitly asks for a class by name — Class.forName("com.x.Driver"), reflection — and it can't be founda class was present when compiling but is missing or failed to initialise at run time
    Typical causea missing driver/plugin JAR; a typo in a class namea dependency JAR missing from the deployment; or a static initialiser that threw (then every later use gives NoClassDefFoundError)

    Check yourself

    4 questions
    1

    Name the three phases of class loading, and the three parts of linking.

    Show answer

    Loading, linking (verification, preparation, resolution), initialisation.

    2

    When does a class's static initialiser run?

    Show answer

    Once, on the class's first active use (instance creation, static method call, non-constant static field access).

    3

    What is parent delegation, and why does it matter for security?

    Show answer

    A loader asks its parent before loading a class itself, so core classes always come from the trusted bootstrap loader and can't be replaced.

    4

    ClassNotFoundException vs NoClassDefFoundError?

    Show answer

    ClassNotFoundException: a class requested by name at run time (e.g. Class.forName) isn't found. NoClassDefFoundError: a class available at compile time is missing or failed to initialise at run time.