Class Loader

Class loader makes JVM to load classes that are being used by an application. Class loading process have three phases:
1. Physical Loading
2. Linking
3. Initializing
As shown in the following figure:
1) Physical Loading
Physical loading is the fi rst phase, in which class loader, searches the required class files on specified paths and loads the bytecode in memory.
2) Linking Linking phase is accomplished in three stages:
a) Bytecode Verification:
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. fields, methods etc.
c) Resolution:
Transforming symbolic references from the type into direct references.
3) Initialization Phase
During initialization phase, static initializer blocks are executed for initializing static fi 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 files) from directories specifi ed by system CLASSPATH variable.
The classloader hierarchy is shown below:

Delegation Hierarchy

Note that the class loader hierarchy shown below is not inheritance hierarchy but a delegation hierarchy. Most class loaders delegate finding classes and resources to their parent class loader before trying to load the classes itself. If the parent class loader cannot find the class or resource, then the class loader try to find 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 first 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 first.


About the Author
Rajesh K. Bansal (SCJP-Sun Certified Java Programmer)
20 Years experience in Training & Development. Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android,MERN Stack(MongoDB,Express,ReactJS,NodeJS). If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email : bcebti@gmail.com #Contact : 98722-46056
Available on Amazon
Card image cap
Under the guidance of Founder & Author of "realJavaOnline.com". M:9872246056
Card image cap