Inside the JVM — Part 02 (ClassLoader)

Chamal Weerasinghe
May 2, 2021 · 5 min read
Photo by Petrebels on Unsplash

“Integrity is doing the right thing, even when no one is watching.” C. S. Lewis

Now let’s review the JVM, Basically, JVM has three main functions, which are to Loading the compiled class file, Store it and then Execute it. To go through this process JVM has three individual components.

The loading process is done by the ClassLoader, the storing will be done by the Memory Area and the program execution will handle by the Execution Engine. Now let’s discuss each and every one of them in full detail.

Now let’s talk about the ClassLoader.

When a JVM instance starts running classloader's basic responsibility is to load the compiled classes into the JVM at the runtime. If the class could not find it will throw ClassNotFoundException . The most important thing to know is the Every Custom ClassLoader is implemented extending the “ClassLoader” class (abstract), which is located in the “java.lang” package other than the Boostrap ClassLoader (Details in below).

There are few principles that every ClassLoader should comply with.

Visibility Principle — Children class Loaders can see the other ClassLoaders generated by their parent ClassLoader, but the Parent ClassLoader cannot see what are the ClassLoaders that its Children ClassLoaders are generated (Grandparents don’t know about the grandkids 😉)

Uniqueness Principle- Make sure that one class loads only once, and ensuring the child class does not load the same class that its parent already loaded.

Delegation Principle — This follows a hierarchical approach to find and load the appropriate class when a request came to load the class (exampleClassLoader.loadClass(“”)), it will pass the request from the lowest level to the upper level (Application ClassLoader > Extension ClassLoader > Bootstrap ClassLoader) to find the class, If it was not found there then it will pass to back to the Extension ClassLoader to find where is it in a custom path if that also failed last to the Application ClassLoader and then throws a “ClassNotFoundException” if not found.

No Unloading Principle — A loaded class cannot be unloaded from the class loader it can only delete and create a new class loader.

There are few different classloaders in JVM, those are

In order to run a Java program, the necessary libraries and the core Java APIs should be loaded, bootstrap class loader also called a primordial class accounts for the responsibility of loading these classes. Boostrap ClassLoader considers a core part of the JVM written in native code used in JRE implementation (C, C++) and the ultimate parent class for the ClassLoaders.

This ClassLoader is the child ClassLoader of the Boostrap ClassLoader, responsible for loading all the extension classes of the standard classes.

This is also known as the System ClassLoader, and child of Extension ClassLoader, this responsible for loading all the application level class files related to the application mentioned in the classpath (-cp) when compiling the program.

The classloader itself consists of three sub-processes, those are Loading, Linking, and Initialization. Let’s now talk about each and every one of them in detail.

ClassLoader Subprocesses

1. Loading

When a class Loading into it first reads few information from the class those are,
The fully qualified class name(Example — “java.lang.String”).
It’s immediate parent class information.
Instance and Static variables, and
Find out whether it's a class, interface, or enum.

After this information, for the first time of JVM running it creates an object from the “Class” file (“java.lang.Class”), most important thing is this object is not create using the class file created in the application (example — User, Employee).

Class class = new AdminUser();

and place it in the heap.


Linking is divided into three subprocesses, those are Verification, Preparation, and Resolution.

2.1 Verification — This process ensures that the security of the byte code, verification process is there to decide whether the given class file is safe to execute and is it altered from its original form. This process follows few criteria with the help of the Byte code verifier, those are

  • Checks whether the compiled class file is coming from a valid compiler, comply with the JVM specification.
  • Checks it has the correct structure of the class file and correct format of language, comply with the rules of syntax and other rules.

If any of these are failed it will throw a VerifyException.

2.2 Preparation — If the class has instance level or static variables, in this phase it will provide the default values (not the values assigned by the user).

2.3 Resolution — When programmers writing the Java programs they are using symbolic names for the objects based on the domain of the application as an example classes like Payment, Student, Course like that. But when it reaches the JVM those are not identified by the JVM or at the machine level, in this phase, it will search the method area to locate the referenced entities and replace them with the reference where they actually reside in the heap.

3. Initialization

As the final step of the ClassLoader, it will decide there are any active uses of the classes or static variables if there are any active uses then it will do the initialization processes, There are few criteria to choose whether it has an active use case.

  • If a class is created using the “new” keyword (new Student()).
  • Invoking a static method in a class considers the class which resides its method as active use.
  • Assigning a value for a static field. the important thing to notice here is the active case will be decided if the field is static, if it defined as “final” or “static final” it is not considered as active use.
  • If the class is the initial class, which means it contains the “public static void main(String[] args)” method.
  • If a class does not have any of these but another class has one or more attributes which are extending a class, then the parent class is also considered as an active use where it can go through the initialization, as an example
public class Student{...} public class FullTimeStudent extends Student { ... }public class Main {  
FullTimeStudent ftStudentGrd = new FullTimeStudent();
//Both the Student and FullTimeStudent classes considered as active use

Java ClassLoader —


Medium’s largest Java publication, followed by 14630+ programmers. Follow to join our community.