Java Class Loaders
Java Class Loaders
Search
Listen Share
When I plunged into the world of Java class loaders, it was a response to a curious
problem. Popular publications, supposed beacons of the Java world, are filled with
conflicting and outdated information on the subject. This discrepancy sparked my
investigation — a quest for clarity in the maze of Java class loaders.
Let’s try to dive in together, stripping away the complexity. Here is a full picture of
what we will explain:
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 1/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Proposition
Before we get further into the mechanics of class loaders, it’s important to
underscore one significant detail:
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 2/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
relatively hyped one, GraalVM (OpenJDK based). Each of these adheres to the JVM
specification but may differ in various ways, including performance characteristics,
garbage collection strategies, and, as you might guess, the details of class loading.
Most articles on this topic also don’t give a specific version of Java when describing
it, which actually leads to misunderstanding, because the JVM evolves and changes
with each version. It’s autumn 2023, and the Java 21 just released, so we’ll focus in it,
relying on Oracle’s JVM specification itself, and the Oracle Java SE documentation
for simplicity.
With this in mind, let’s get back to our explo ration of the Java ClassLoader system.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 3/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
But what do JVM loads really mean? Java programs are comprised of classes and
interfaces, written in human-readable Java code. To run this code on a machine, it
needs to be translated into machine-understandable bytecode. This bytecode is
stored in .class files, which the JVM can read and execute.
So when we talk about “loading a class”, we are referring to the process of finding
the appropriate .class file with a particular name, reading its contents, and
bringing it into the JVM’s runtime environment, which is a specific portion of your
machine’s memory dedicated to running your application.
Or, if you prefer, a slightly more formal definition of “loading” from Oracle:
Loading refers to the process of finding the binary form of a class or interface with a
particular name, perhaps by computing it on the fly, but more typically by
retrieving a binary representation previously computed from source code by a Java
compiler, and constructing, from that binary form, a Class object to represent the
class or interface.
flexibility to load classes from various sources — not just the local file system, but
also over a network, from a database, or even ones generated on the fly. Let’s deep
dive, breaking down the steps.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 5/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
The Loading sub-system isn’t a solo act; it’s a hierarchical relay. Each ClassLoader,
parent, and child, operate together, passing the baton of responsibility until the
correct class is loaded.
The fundamental principles guiding this coordinated class loading process are:
1. Visibility: a child ClassLoader can see classes loaded by its parent, but not vice
versa, ensuring encapsulation;
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 6/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Bootstrap ClassLoader
The oldest member of the family, the Bootstrap ClassLoader, is in charge of loading
the core Java libraries located in the <JAVA_HOME>/jmods folder (such as java.lang.,
java.util., etc.) required by the JVM. Looking at the diagram, one can observe that
other ClassLoaders are written in Java (objects of java.lang.ClassLoader), implying they
also need to be loaded into the JVM — a task undertaken by the Bootstrap
ClassLoader as well.
It’s also worth noting that many resources describe the Bootstrap ClassLoader as the
“parent” of the remaining classloaders. This signifies a logical inheritance rather
than direct Java inheritance since Bootstrap ClassLoader is written in native code.
This could be easily confirmed by the following line of code:
jshell> System.out.println(java.lang.ClassLoader.class.getClassLoader());
null
Bootstrap ClassLoader is also the only ClassLoader explicitly described in the Oracle
specification. Definition of the remaining ones is called “User-defined” and left to the
discretion of specific VM vendors.
Platform ClassLoader
In my opinion, is the most controversial.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 7/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Platform ClassLoader
The platform class loader is responsible for loading the platform classes. Platform
classes include Java SE platform APIs, their implementation classes and JDK-
specific run-time classes that are defined by the platform class loader or its
ancestors. The platform class loader can be used as the parent of a ClassLoader
instance.
But what differentiates the platform classes from the core classes loaded by the
Bootstrap ClassLoader? Let’s attempt to observe what it essentially loads:
jshell> ClassLoader.getPlatformClassLoader().getDefinedPackages();
$1 ==> Package[0] { } // empty
It turns out that in an entirely empty Java program — absolutely nothing! Now, let’s
try to explicitly use a class from some standard package:
jshell> java.sql.Connection.class.getClassLoader()
$2 ==> jdk.internal.loader.ClassLoaders$PlatformClassLoader@27fa135a
jshell> ClassLoader.getPlatformClassLoader().getDefinedPackages()
$3 ==> Package[1] { package java.sql }
So to put it simply, Bootstrap loads the core runtime classes required to start the
JVM, while the Platform loads the public types of the system modules, which a
developer might need.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 8/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Application ClassLoader
This classloader is responsible for loading all the classes from the classpath that
has been set. This could be from directories, JAR files, or other sources that have
been specified in the classpath. It is here that most user-defined code is loaded
when a Java application is launched.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 9/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
}
}
In addition to the three primary Class Loaders that we have discussed, you can also
create your own User-Defined Class Loaders directly within their code. This feature
provides an avenue for ensuring application independence, facilitated by the class
loader delegation model. Web application servers such as Tomcat utilize this
approach to ensure that different web applications and enterprise solutions can
operate independently, even if they’re hosted on the same server. We won’t focus on
that, since there are already enough guides on the topic of custom creation.
Worth mentioning, that every Class Loader maintains its own namespace that
records the classes it has loaded. When a ClassLoader is tasked with loading a class,
it first consults this namespace, searching for the Fully Qualified Class Name
(FQCN) to determine whether the class has already been loaded. Intriguingly, even
when a class shares an identical FQCN with another if they exist in different
namespaces, they are considered distinct classes. Having a class in a different
namespace implies that it was loaded by a different ClassLoader, reinforcing the
autonomy and separation between different parts of an application.
essential tasks to verify the integrity of the code, prepare it for execution, and
resolve any dependencies it might have.
Once a class is loaded, it goes through a phase called linking. This phase involves a
series of steps:
Verification
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 11/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Verification phase
This stage is critical to maintaining the robustness of the Java runtime environment.
It view the bytecode of the class or interface to ensure its structural correctness,
compatibility with the JVM, and validation that it has been generated by a
legitimate compiler.
In a world where Java programs can be transported over networks and potentially
be generated by hostile compilers, this process becomes of paramount importance.
It checks for consistency in the symbol table, whether final methods or classes have
been overridden incorrectly, the correctness of access control keywords, the
accurate number and type of parameters, correct stack manipulation, and more.
Preparation
Preparation phase
During this step, the JVM allocates memory for the class or interface’s static
variables and initializes them with their default values.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 12/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
If the class or interface has instance fields, these too are allocated with memory and
assigned default values, once the static fields of the entire class hierarchy are
addressed. This preparation step allows for efficient runtime performance by laying
the groundwork for the program’s execution.
Because linking involves the allocation of new data structures, it may fail with an
OutOfMemoryError .
Resolution
Resolution phase
Here, any symbolic references in the class or interface — which are logical
references that point to other classes or interfaces — are replaced with their actual
memory locations. This transformation from symbolic references into direct
references, often referred to as Dynamic Linking, ensures that all dependencies of a
class or interface are available at runtime.
Interestingly, this step can be performed “lazily”, that is, only when a statement with
a symbolic reference is executed. This approach, used by most JVMs, conserves
resources as it prevents the unnecessary loading of classes or interfaces that may
never be invoked.
3. Initialization
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 13/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Initialization phase
Here, the initialization logic of each loaded class or interface will be executed (e.g.
calling the constructor of a class). Since the JVM is multi-threaded, the initialization
of a class or interface must be synchronized to prevent simultaneous initialization
by multiple threads, ensuring thread safety.
The JVM invokes the special <clinit> method (the bytecode version of your static
blocks and variable assignments), setting all static variables to their designated
initial values. At this point, the class is finally ready to be employed.
And there we go: application classes have been found, linked, initialized, and are
now ready to be integrated into the bigger picture. The JVM now steps back, leaving
the stage to your application. The classes, brimming with functionality and
interconnected in an intricate web, are primed to breathe life into your application.
Classes now can be created and manipulated, with methods invoked and variables
set as defined by the logic of your app.
But we’ll leave the later JVM stages for later articles.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 14/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
I hope all this information is making sense, and you caught something interesting
from there. We’ve dived deep into the mechanisms and nuances, but this article is
not pretending to be the only source of truth. Feel free to share, comment and
correct to polish this article down to a better version.
And subscribe to my Twitter so you don’t miss anything new, stay in touch!
Appendix
Oracle’s Java SE API Documentation: java.lang.ClassLoader
Subscribe
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 15/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Responses (3)
Write a response
Yashodha Hettiarachchi
Jul 3, 2024
I was scouring the internet to understand how pf4j works under the hood and found this masterpiece !!
Thank you writing this
2 Reply
Nitin Agrawal
Dec 8, 2023
This means that when a class is loaded, and it’s not found by the Application
ClassLoader, the request is delegated upwards to the Platform ClassLoader and
if necessary, further up to t...
I think, it goes in reverse order & if class is not found by ApplicationClassLoader then we get
'ClassNotFfoundException'
2 1 reply Reply
Nitin Agrawal
Dec 8, 2023
I think, it is 'ClassDefNotFound', as class was there during compilation but .class file not present during
linking.
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 16/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
1 1 reply Reply
Yahor Barkouski
Apr 9, 2022
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 17/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Java Interview
Feb 21 479 20
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 18/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Mar 2 824 31
Umadevi R
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 19/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Hui
Apr 3 394 14
Rishi
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 20/21
4/11/25, 3:08 PM Java Class Loaders: Explanation in Diagrams | Yahor Barkouski | Medium
Melih Firat
https://medium.com/@wakefulinsomnia/how-does-java-classloader-system-work-82eaf378f73b 21/21