How ClassLoader works in java

How ClassLoader works in java?



To understand how ClassLoader works in java, first we need to understand

What is the ClassLoader in Java?



ClassLoader is an abstract class and extends Object class, A ClassLoader object is an object which is responsible for loading classes. The class ClassLoader is an abstract class. Every Class object contains a reference to the ClassLoader which defined it.

It is basically a mechanism in which class files are loaded in Java. ClassLoader is responsible for loading class files from file system, network etc.


So in very simple Language Java ClassLoader loads a Java class file to Java Virtual Machine and that class object has the reference of the ClassLoader who has helped to load it in the Java Virtual Machine.


So if you want to go more deeper lets see what are the different types of ClassLoaders available in java?

Bootstrap Class Loader



Bootstrap ClassLoader is also called Primordial ClassLoader in Java, this type of ClassLoader helps to load Java's core classes such as java.lang, java.util and it takes those files from location JRE/lib/rt.jar

Extensions Class Loader



It loads the classes from /jre/lib/ext folder, which are the extensions of standard Java classes.

System Class Loader



All the classes available from java classpath provided are loaded with the help of System Class Loader.

There are some other extended type of ClassLoaders also available which have specific tasks such as to load URL Classes etc.


How ClassLoader works in java?




So far we have seen what the ClassLoader is , now we will see how ClassLoader works in java?

Every class has its name, ClassLoader's first task is to locate that class and reads a class file of from the native location and no doubt ClassLoader's work is dependent on platform. ClassLoader class has a method name defineClass which takes input as byte array and loads a class.

All ClassLoaders except the class loaders of type bootstrap class loader has a parent. Here you need to know is every ClassLoader instance is associated with its parent.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment