Class loader makes JVM to load classes that are being used by an application. Class loading process have three phases:
1. Physical Loading
As shown in the following ﬁgure:
1) Physical Loading
Physical loading is the ﬁ rst phase, in which class loader, searches the required class ﬁles on speciﬁed paths and loads the bytecode in memory.
2) Linking Linking phase is accomplished in three stages:
a) Bytecode Veriﬁcation:
3) Initialization Phase
Class loader performs various checks on bytecode to ensure its proper formation, as required by JVM.
b) Class Preparation:
In this stage, memory is allocated to components of a class
i.e. ﬁelds, methods etc.
Transforming symbolic references from the type into direct
During initialization phase, static initializer blocks are executed for initializing static ﬁ elds to their default values.
Types of Class Loaders
When JVM starts, three class loaders work:
1. Bootstrap class loader
2. Extension class loader
3. Application class loader
1. Bootstrap Class Loader:
It is responsible for loading Core java library (rt.jar) from JDKfolder/jre/lib directory. This class loader is a program written in native code.
2. Extension Class Loader:
It is responsible for loading class libraries (x.jar) from JDKfolder/jre/lib/ext directory.
3. Application Class Loader:
It is responsible for loading classes (jar ﬁles) from directories speciﬁ ed by system CLASSPATH variable.
The classloader hierarchy is shown below:
Note that the class loader hierarchy shown below is not inheritance hierarchy but a delegation hierarchy. Most class loaders delegate ﬁnding classes and resources to their parent class loader before trying to load the classes itself. If the parent class loader cannot ﬁnd the class or resource, then the class loader try to ﬁnd them locally. In other words, a class loader loads the classes which are not available to the parent class loader. For example, if the application class loader need to load a class, it ﬁrst delegates to the extension class loader, which, then delegates to the bootstrap class loader. However, the parent class loader always has the opportunity to load a class ﬁrst.