# what is classloader in java?

ClassLoader in Java is a class that is used to load class files in Java. Java code is compiled into the class file by javac compiler and JVM executes Java program, by executing byte codes written in the class file.

ClassLoader is responsible for loading class files from file system, network or any other source. There are following three default class loader in java:

## Types of ClassLoaders in Java:

Being a Machine code, a Bootstrap Classloader kickstarts the operation when the JVM calls it. It loads the first pure Java ClassLoader but is not a java class. The classes are loaded from the location rt.jar by the Bootstrap ClassLoader. Often called the Primordial ClassLoader, it doesn’t have any parent ClassLoaders.

The extensions of core java classes are loaded from the respective JDK Extension library by the Extension ClassLoader. It is a child of Bootstrap ClassLoader. The file is loaded by the Extension ClassLoader from the jre/lib/ext directory or any other directory. The directory is pointed by the system property java.ext.dirs.

Often known as a System ClassLoader and being a child class of Extension ClassLoader, an Application ClassLoader loads the Application type classes. These classes are found in the environment variable CLASSPATH, -classpath or -cp command-line option.

## Order of functioning of the ClassLoader Delegation Hierarchy Model:

The order of the functioning of the ClassLoader Delegation Hierarchy Model is:

Here, the highest property is given to the Bootstrap ClassLoader, then to Extension ClassLoader and the last priority is given to the Application ClassLoader.

## Principles of the functionality of a Java ClassLoader:

The set of rules or features on which a Java ClassLoader works are known as the Principles of functionality. A Java ClassLoader has three principles of functionality. These are:

### Delegation Model:

The Delegation Hierarchy Algorithm is an algorithm that is used by the Java Virtual Machine and the Java ClassLoader to Load the classes into the Java file.

The delegation model provides a set of operations based on which the ClassLoader works. These set of operations are given below:

1. The Delegation Hierarchy Principle is always followed by the ClassLoader.
2. JVM checks whether a class is already loaded or not when it comes across a class.
3. JVM proceeds with execution only when the Class is already loaded in the method area.
4. JVM asks the Java ClassLoader Sub-System to load the specific class, in case the class is not present in the method area. Then the control is handed to the Application ClassLoader by the ClassLoader sub-system.
5. The request is then delegated to the Extension ClassLoader by the Application ClassLoader. The request is then delegated to the Bootstrap ClassLoader by the Extension ClassLoader.
6. The Bootstrap ClassLoader searches for the class in the Bootstrap classpath(JDK/JRE/LIB). If present, the class is loaded. The request is delegated to the Extension ClassLoader in case the class is not present.
7. The class in the Extension Classpath(JDK/JRE/LIB/EXT) is searched by the Extension ClassLoader. It is loaded, in case the class is available. And in case the class is not available, the request is delegated to the Application ClassLoader.
8. The class in the Application Classpath is searched by the Application ClassLoader. It is loaded, in case the class is available. And in case the class is not available, a ClassNotFoundException exception is generated.

### Uniqueness Property:

To ensure that all the classes are unique and without any repetition, the Uniqueness Property is utilized. It thus also provides the guarantee that the classes loaded by parent classloaders are not loaded by the child classloaders. The current instance would attempt to find the class, only when the parent class loader isn’t able to find it.

To load a class, the JVM first requests for the class, after which a few more steps are followed. In loading a Class, a vital role is also played by some important Methods or Functions, even though the Classes are loaded as per the delegation model.

To load the classes, referenced by the JVM, the loadClass(String name, boolean resolve) is used. The name of the class is taken as a parameter, by this method. It is of type loadClass(String, boolean).

defineClass() method:

To define an array of bytes as an instance of the class, the defineClass() method is used. Being a final method, cannot be overridden. The ClassFormatError is displayed in case the class is invalid.

findClass(String name) method:

To find a specified class, the findClass(String name) is used. It, however, doesn’t load the class.

To verify whether the Class referenced by the JVM was previously loaded or not, the findLoadedClass(String name) is used.

protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class a = findLoadedClass(name); try { if (a == NULL) { if (parent != NULL) { a = parent.loadClass(name, false); } else { a = findBootstrapClass0(name); } } catch (ClassNotFoundException e) { System.out.println(e); } } }