Types of Class Loader | Delegation Algorithm | JVM Internals
In previous articles, we learned about JVM architecture and one of its component Class Loader SubSystem. In this article, we will discuss about Types of Class Loaders and How Class Loader uses Parent delegation Algorithm to load the class. If you have not gone through previous article on JVM Internals, I would encourage you to read that first.
In interview room, we often come across the questions related to JVM architecture like,
- Which class loading mechanism or algorithm JVM uses to locate the class or
- What are the Types of Class Loaders JVM uses to load classes into Method area and so on.
You will find answer of all such Class Loader related questions in this article. Stay tuned.
The mechanism that enabled dynamic loading in Java is a class loader which is one of the cornerstones of Java dynamism. Class loaders are responsible for determining when and how classes can be added to a running Java environment, as well as making sure that important parts of the Java runtime environment are not replaced by impostor code.
You can also develop a custom class loader that is, capable of checking a digital signature before executing untrusted foreign code.
In this article, we will discuss about following
Types of Class Loader
Class Loader Subsytem contains following three types of class Loaders
- Bootstrap Class Loader or primordial Class Loader
- Extension Class Loader
Application Class Loader or System Class Loader
Bootstrap Class Loader
It is responsible to load core Java API classes i.e. the classes present in rt.jar, which is present at path
- This location is called bootstrap class path i.e. Bootstrap Class Loader is responsible to load classes from bootstrap class path.
- Bootstrap Class Loader is by default available with every JVM.
It is implemented in native languages like C / C++ and not implemented in java.
Extension Class Loader
- It is the child class of Bootstrap Class Loader.
It is responsible to load classes from extension class path
It is implemented in java and the corresponding .class file is
Application Class Loader / System Class Loader
- It is the child class of Extension Class Loader.
- This class loader is responsible to load classes from application class path.
- It internally uses environment variable classpath.
It is implemented in java and the corresponding .class file name is
How Class Loader works
- ClassLoader follows delegation hierarchy algorithm. Whenever JVM come across particular class, first it will check whether the corresponding .class file is loaded or not.
- If it is already loaded in method area, then JVM will consider that loaded class.
- If it is not loaded then JVM request class loader sub sytem to load that particular class.
- Then class loader subsystem handovers the request to Application class loader.
- Application class loader delegates the request to Extension class loader which in turn delegates the request to Bootstrap class loader.
- Then Bootstrap class loader will search in Bootstrap class path If it is available then the corresponding .class will be loaded by Bootstrap class loader
- If it is not available then Bootstrap class loader delegates the request to Extension class loader.
- Extension class loader will search in Extension class path. If is is available, then it will be loaded otherwise extension class loader delegates the request to application class loader.
- Application class loader will search in Application class path. If it is available, then it will be loaded otherwise we will get runtime exception saying ClassNotFoundException.
Important Point to Note:
- Classes loaded by a child class loader have visibility into classes loaded by its parent class loaders. So classes loaded by System ClassLoader have visibility into classes loaded by Extensions and Bootstrap ClassLoader.
That's all for this topic. If you guys have any suggestions or queries, feel free drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
We will be including more articles in JVM Internals Series. Happy Learning 🙂
Further Reading For interview Preparation
- JVM architecture Introduction
- Class Loader SubSystem
- Java Program to find from where java class is loaded
- interface VS abstract class
- Access Modifiers (public, private, protected and default scope)
- HashMap vs Hashtable Vs SynchronizedMap Vs ConcurrentHashMap
- CountDownLatch Vs CyclicBarrier
- How To Create Immutable Class In Java
- What is the use of private constructor in java?
- String Pool
Comments and QueriesIf you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.