Sitemap

ClassLoader in Java

2 min readJul 18, 2022

--

A ClassLoader is an object that is responsible for loading classes. It dynamically loads Java classes into the JVM. But it does more work than that. There are mainly two types of class loaders in JVM. The first one is BootStrap ClassLoader, and the second one is Custom-defined ClassLoader. Behind the scenes, ClassLoader is mainly responsible for three tasks.
1. Loading
2. Linking
3. Initialization

Get Suneth Pinidiya’s stories in your inbox

Join Medium for free to get updates from this writer.

The loading phase’s main responsibility is to take your class file and put it into the memory. The linking phase is again divided into three sub-steps — Verification, Preparation, and Resolution. The verification phase is very important. Java has a bytecode verifier in JVM. There it will exactly check whether the class is safe to execute or not. That’s why you probably heard like Java is safe to execute in any environment. JVM can make sure this is safe to execute. When you are trying to load a java class, there is a subprogram called bytecode verifier, which will verify whether it comes from the valid compiler, whether it has corrected structure and whether the class file has correct formatting. Suppose any of these are not satisfied, JVM will throw the Runtime Exception. If everything is done, the verification process is complete, and then it moves to the Preparation process. This preparation phase will assign a default value if you use any instance level or static variable in your class. In the Resolution phase, JVM locates classes, interfaces, fields, and methods referenced in the constant pool and determines the concrete values from their symbolic reference. Initialization of a class or interface consists of executing its class or interface initialization method. The initialization process involves the following two functions. Those are initializing class variables with the routine specified by the programmer and initializing its superclasses if it is not already initialized.

--

--

Suneth Pinidiya
Suneth Pinidiya

Written by Suneth Pinidiya

Associate Software Engineer at Virtusa