Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Explain class loading in Java 9 and later: the built-in class loaders, delegation, and the phases a class goes through before use.

Java 9 and later have three built-in class loaders: the bootstrap loader for the core java.base classes, the platform loader, which replaced the old extension loader, and the application or system loader for our class path and modules. Each loader asks its parent first, so core classes are always loaded by the bootstrap loader. A class then goes through loading, linking (verification, preparation of static fields, and resolution) and initialization, which runs its static initializers on first active use, such as creating an instance or calling a static method. At runtime a class is identified by its name plus its defining class loader.

Why this question is asked

Class loading usually works silently, so most developers only meet it through errors: ClassNotFoundException, NoClassDefFoundError, a static initializer that runs at a surprising moment, or the bizarre ClassCastException saying a class cannot be cast to itself. Each of those makes sense once we know how loaders delegate and when each phase happens. Interviewers ask this to test that understanding, including what changed with the module system in Java 9. In this answer, we will meet the three built-in loaders, see how delegation works, follow a class through its phases, see exactly when initialization happens, and end with why the loader is part of a class's identity.

1The Three Built-In Class Loaders

A class loader is an object that finds the bytes of a class and turns them into a Class. Since Java 9 the JDK is split into modules, and the built-in loaders are organised around them. The bootstrap loader is part of the JVM itself and loads the most fundamental modules, such as java.base with java.lang.String and java.util.List. It has no Java object, so asking a core class for its loader returns null.

The platform loader loads the other Java SE and JDK modules that are not in the bootstrap set, for example java.sql. It replaced the extension loader of Java 8, which loaded JARs from the jre/lib/ext folder; that mechanism was removed. The application loader, also called the system loader, loads our classes from the class path and the module path.

Java
System.out.println(String.class.getClassLoader()); // null: bootstrap
System.out.println(java.sql.Connection.class.getClassLoader().getName()); // platform
System.out.println(MyApp.class.getClassLoader().getName()); // app
System.out.println(ClassLoader.getSystemClassLoader().getParent().getName()); // platform

2Delegation: Asking the Parent First

When a loader is asked for a class, it first checks whether it has already loaded it. If not, it delegates the request to its parent, and only if the parent cannot find the class does it try itself. The application loader's parent is the platform loader, whose parent is the bootstrap loader. So a request for java.lang.String from anywhere ends at the bootstrap loader.

The reason for this design is safety and consistency. If our class path contained a fake java.lang.String, it would never be used, because the bootstrap loader finds the real one first. And every part of the application sees the same String class. Since Java 9, delegation is also module-aware: when a package belongs to a named module that is mapped to a particular loader, the request is sent directly to that loader, but the effect is the same, core packages always come from the JDK.

Step throughLoading com.example.Order with the app loader
  1. App loader: already loaded?no
  2. Delegate to platform loaderit delegates to bootstrap
  3. Bootstrap finds it in its modules?
    YesReturn the JDK classNoPlatform tries its modules
  4. Platform finds it?
    YesReturn itNoApp loader searches class path and module path
  5. App loader finds the bytes?
    YesDefine the classNoClassNotFoundException

5 steps in order. Play them, or step through them one at a time.

3The Phases: Loading, Linking, Initialization

Loading reads the bytes, for example from a JAR, parses the class file format and creates the in-memory class in metaspace, along with its java.lang.Class object. Superclasses and interfaces are loaded too.

Linking has three steps. Verification checks that the bytecode is safe: types are used correctly, the stack never overflows within a method, jumps land on real instructions. Preparation allocates the static fields and sets them to default values, 0, false or null; no user code runs yet. Resolution replaces symbolic references, such as the name of another class or method in the constant pool, with direct references, and HotSpot does this lazily, when each reference is first used.

Initialization runs the class's static initializer, the code from static field assignments and static blocks, in the order they appear in the source. Only now do static fields get their real values. The JVM guarantees this happens exactly once, even when several threads trigger it at the same time; the other threads wait.

Step throughFrom bytes to a usable class
  1. Loadingfind bytes, parse, create the class in metaspace
  2. Verificationis the bytecode type-safe and well formed?
  3. Preparationstatic fields set to 0, false or null
  4. Resolutionsymbolic references to direct ones, lazily
  5. Initializationrun static initializers, exactly once

5 steps in order. Play them, or step through them one at a time.

4When Exactly Initialization Happens

Initialization is triggered by the first active use of a class: creating an instance with new, calling a static method, reading or writing a static field that is not a compile-time constant, initialising a subclass, reflective calls such as Class.forName(name) with its default behaviour, or being the main class. Merely mentioning the class, declaring a variable of its type or reading its .class literal does not initialise it.

Java
class Config {
static final int VERSION = 3; // compile-time constant, inlined by javac
static final Instant LOADED = Instant.now(); // not a constant
static { System.out.println("Config initialised"); }
static void touch() { }
}
Class<?> c = Config.class; // nothing printed: no initialization
Config[] arr = new Config[10]; // nothing printed: arrays do not initialise the class
System.out.println(Config.VERSION); // 3, and still nothing printed: the value was copied at compile time
System.out.println(Config.LOADED); // "Config initialised", then the instant
Config.touch(); // nothing printed: already initialised

Notice the compile-time constant. javac copies the value 3 into the calling class, so reading Config.VERSION never touches Config at runtime. This is also why changing a public constant in a library requires recompiling the code that uses it. The lazy, thread-safe initialization is what makes the holder idiom for singletons work: a nested class holding the instance is initialised only when first accessed.

5Name Plus Loader: A Class's Real Identity

At runtime a class is not identified by its name alone but by its name together with the loader that defined it. Two loaders can each define a class called com.example.Order from the same bytes, and the JVM treats them as two different, incompatible types.

Java
URL[] jar = { Path.of("plugin.jar").toUri().toURL() };
ClassLoader l1 = new URLClassLoader(jar, null); // parent null: no delegation to the app loader
ClassLoader l2 = new URLClassLoader(jar, null);
Class<?> a = l1.loadClass("com.example.Order");
Class<?> b = l2.loadClass("com.example.Order");
System.out.println(a.getName().equals(b.getName())); // true
System.out.println(a == b); // false: different defining loaders
Object o = a.getDeclaredConstructor().newInstance();
b.cast(o); // ClassCastException: Cannot cast com.example.Order to com.example.Order
// (a plain cast gives: class com.example.Order cannot be cast to class com.example.Order ... loader names follow)

This is the source of the famous 'cannot be cast to itself' error in application servers, plugin systems and hot-reload tools, where the same class ends up loaded by two loaders. The full message usually names both loaders, which points straight at the cause. It is also what makes isolation possible: each web application in Tomcat can have its own version of a library.

6Follow-up questions

What is the difference between ClassNotFoundException and NoClassDefFoundError?

ClassNotFoundException is thrown when code explicitly asks a loader for a class by name, for example with Class.forName or loadClass, and no loader finds it. NoClassDefFoundError is thrown when running code that was compiled against a class needs it and the JVM cannot load it now, or when an earlier attempt to initialise that class failed; in that second case the message often says Could not initialize class.

Why does the extension mechanism no longer exist?

In Java 9 the JDK was modularised and the old extension directory mechanism was removed. Code that relied on dropping JARs into jre/lib/ext must put them on the class path or module path instead. The platform class loader took its place in the hierarchy, but it loads JDK modules, not arbitrary JARs.

What does the thread context class loader do?

It is a loader attached to each thread that frameworks use when they need to load classes that the framework's own loader cannot see, for example a JDBC driver or a ServiceLoader provider in the application. Application servers set it to the web application's loader during a request. It is a deliberate escape from strict parent-first delegation.

7Conclusion

In this answer, we met the three built-in loaders of Java 9 and later, bootstrap, platform (which replaced the extension loader) and application, and saw that parent-first delegation, now module-aware, means core classes always come from the bootstrap loader. We followed a class through loading, linking with verification, preparation and resolution, and initialization, saw that initialization runs once on first active use while constants and .class literals do not trigger it, and learned that a class's identity is its name plus its defining loader, which explains the cast-to-itself error.

What to say in the interviewAbout 90 seconds aloud

Open with

Since Java 9 there are three built-in loaders. The bootstrap loader is part of the JVM and loads core modules like java.base, which is why String.class.getClassLoader returns null.

Then, if they want more

The platform loader loads the other JDK modules, such as java.sql, and replaced the old extension loader and its lib/ext folder. The application or system loader loads our class path and module path. Loading uses delegation: a loader checks whether it has the class, then asks its parent first, and only searches itself if the parent fails.

In Java 9 that became module-aware, but the effect is that core classes always come from the bootstrap loader, so nobody can replace java.lang.String.

A class then goes through loading, where the bytes are parsed and the class is created in metaspace; linking, which is verification of the bytecode, preparation of static fields with default values, and resolution of symbolic references, done lazily in HotSpot; and initialization, which runs the static initializers exactly once, thread-safely.

Initialization happens on first active use, like new, calling a static method, or reading a non-constant static field, but not on a .class literal or on reading a compile-time constant, which javac inlines.

Finally, a class's identity is its name plus its defining loader, so the same class loaded by two loaders gives a ClassCastException that looks like a class cannot be cast to itself.

About this answer

Last updated
12 November 2026
Versions it names
Java 9, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain class loading in Java 9 and later: the built-in class loaders, delegation, and...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/79090e1363/