Bonjour à tous et à toutes!

A few weeks ago, I was reading about ClassLoaders in Java. In the beginning, I thought it would be easy and that I could read it quickly, but what do you think? Was I right? No, of course not; it was a bit challenging.

So I decided to share what I learned with you and explain it in a plain and easy way. Also, I am still not a Java expert (I am trying to be), so please let me know if you think I am wrong somewhere or if my explanation is not clear.

What is a ClassLoader?
Java is a very common programming language with a unique way of loading class files into the Java Virtual Machine (JVM). This is done by a component called the class loader, which manages how classes are found and brought into memory. Class loaders handle the process of dynamically bringing Java classes into the JVM when they are needed. This makes Java different from languages like C, where programs are compiled directly into machine code and executed by the operating system.

When your code uses a class for the first time, like com.example.MyClass, Java has to find the bytecode of MyClass.class, which is usually inside a JAR file or a folder.

Different types of class loaders:

Class loaders types

As you can see in the picture above, we have three types of class loaders:

Bootstrap class loader
Platform class loader
Application class loader

The JVM's built-in class loader is called the "bootstrap class loader". It does not have a parent, but it is the parent of the other class loaders. It loads the core Java classes, like Object and String. In simpler words, the bootstrap class loader is written in native code (not in Java), and without it, no Java code could run at all.

The platform class loader is the child of the bootstrap class loader, and it takes care of loading some of the other standard Java modules, for example java.sql.

And the last one is the application class loader, which is responsible for loading all application-level classes into the JVM. The parent of the application class loader is the platform class loader.


Difference between Java 8 (and earlier) and Java 9 (and later):

Older tutorials talk about the extension class loader, which loaded classes from jre/lib/ext. In Java 9 (released in 2017), this was removed and replaced with the platform class loader. The main idea is that the three levels stay the same, but the implementation is different.

Java 8 vs Java 9


Three principles for loading the class:
Delegation:
Imagine Java wants to load a class. The loader does not go and look for the class directly. First, it asks its parent, and the parent asks its own parent. If none of the parents can find the class, the loader searches for it by itself.

Visibility: If a class has been loaded by a child loader (e.g., the application class loader), the parent cannot see it. But if a class has been loaded by a parent (e.g., the platform class loader), the child (the application class loader) can see it.

Uniqueness: If a class has already been loaded by a parent, it is not loaded again by a child. Core classes exist only once.

Delegation concept

Let's see it in code:

Okay, enough theory! Now let's ask Java directly which class loader loaded which class.

While I was reading about class loaders, I found a very nice example in Baeldung's article "Class Loaders in Java". I ran it in Eclipse with JDK 26, but instead of their class, I used my own Child class. I also added some comments to make each line clear:

The demo code (based on Baeldung's example)

And this is the output I got:

Output on JDK 26

So what does it tell us?

The first two lines are the simple part: we just ask Java for the platform and the application class loader, and it gives them to us.

The next three lines are more interesting, because here we ask a class: "Who loaded you?"

  • Child is my own class, so the application class loader loaded it.
  • DriverManager is in the java.sql module, so the platform class loader loaded it, exactly like the table above.
  • ArrayList is a core Java class, so the bootstrap class loader loaded it. But why does it print null? Remember, the bootstrap class loader is written in native code, not in Java. There is no Java object to show us, so Java returns null.

One more small thing I noticed: look at the numbers after the @. "System Classloader" and "Classloader of this class" have the same number, and the same goes for "Platform Classloader" and "Classloader of DriverManager". This means they are not just the same type of loader; they are exactly the same object. A running Java program has one platform class loader and one application class loader, and all classes use the same ones.

Two hierarchies, not one

After I wrote the first version of this post, my professor drew this diagram for me, and it changed how I looked at class loaders:

Two hierarchies: classes (top) and objects (bottom)

It looks like one diagram, but there are actually two different hierarchies in it.

The top part is about classes. These boxes are real Java classes, and the arrows mean "extends". ClassLoader is the root of all class loaders, and it has the main logic, like loadClass() and delegation. SecureClassLoader adds security checks, and BuiltinClassLoader has the shared code of the JDK's own loaders. Finally, the three loaders we know are at the bottom: BootClassLoader, PlatformClassLoader, and AppClassLoader. The "..." means there are more classes at that level, for example URLClassLoader. You can see this hierarchy in Eclipse yourself: select ClassLoader and press F4 (Type Hierarchy).

The bottom part is about objects. These ellipses are the real class loader objects that exist when the program runs. In UML, "b : BootClassLoader" means "an object called b, of type BootClassLoader". The dashed arrows connect each object to its class. The orange arrows are the "parent" links, and this is the delegation chain from before: a asks p, and p asks b.

Under each object there is a list of the classes it has loaded. The bootstrap loader has loaded Object, String, and List, the platform loader has loaded DriverManager, and the application loader has loaded my own classes. So each loader has its own list, and that is why the same class name can exist twice in two different loaders.

And here is the part that surprised me: the platform loader is the parent of the application loader, but in the Java code, they are siblings, because both of them extend BuiltinClassLoader. So "parent" in class loaders does not mean "superclass". They are two completely different things:

Same loaders, two different relationships

My own class loader

Now that I knew the difference between the two hierarchies, I wanted to write my own class loader and see where it fits in each of them.
In the top part (classes), my class simply extends ClassLoader. I only override findClass(), which is the method that runs when the parents cannot find the class. It reads the .class file from a folder and turns the bytes into a class with defineClass():

In the bottom part (objects), I have to choose a parent for my loader. You can see it in the constructor: super(parent). So I did a small experiment: I created two loaders from the same class, with two different parents, and asked both of them to load my Child class.

class1 is the Child class loaded by the normal application class loader, and I compare everything with it.

And this is the output:

Case 1: the parent is the application class loader. I asked myLoader1 to load Child, but look at the output: it was loaded by AppClassLoader, not by my loader! Because of delegation, my loader first asked its parent, and the application class loader found Child in the class path. So my findClass() was never called. And class1 == c1 is true, because it is exactly the same class. This is the uniqueness principle: the class is loaded only once.

Case 2: the parent is null. null means my loader's only parent is the bootstrap class loader. The bootstrap loader only knows core Java classes, so it cannot find Child. Then my own findClass() runs, reads bin/package1/Child.class, and defines the class itself. This time class1 == c2 is false!

But why false? It is the same name and the same .class file. The reason is that for the JVM, a class is not only its name. A class is its name + the class loader that loaded it. Since c2 was loaded by a different loader, the JVM sees it as a completely different class. Each loader has its own list of classes, exactly like the boxes at the bottom of my professor's diagram.

So by choosing the parent, I decided whether my class loader respects the uniqueness principle or not.

Conclusion

So, who loads classes in Java? Now I can answer my own question! There are three class loaders: bootstrap, platform, and application. When a class is needed, the loader first asks its parent, and only if no parent can find it, it searches by itself.

The two things that surprised me the most were:

  • A class is its name + its class loader. The same .class file loaded by two different loaders becomes two different classes, as we saw in my experiment.
  • "Parent" is not "superclass". The delegation hierarchy (objects) and the inheritance hierarchy (classes) are two different things, and my professor's diagram helped me see this.

I started this topic thinking it would be easy, and it was not! But now, when I see an error like ClassNotFoundException, or when I work on modularizing Java projects, I understand much better what is happening behind the scenes.

Again, I am still learning, so if you think something here is wrong or not clear, please let me know in the comments. 🙂



References