What is a class loader and what are the types of the class loader in java?

By | March 5, 2023

In Java, a class loader is a subsystem of the Java Virtual Machine (JVM) that is responsible for loading Java classes into the JVM. The class loader is responsible for finding the class files on the disk or network, loading them into memory, and linking them to form a class.

There are three types of class loaders in Java:

Bootstrap class loader: This is the first class loader to load classes in Java. It is responsible for loading the core Java classes that are part of the Java runtime environment, such as java.lang package. The bootstrap class loader is implemented in native code and is not written in Java.

Extension class loader: This class loader loads classes that are part of the Java extension mechanism. Extensions are optional packages that extend the functionality of the Java runtime environment. The extension class loader loads classes from the jre/lib/ext directory.

System class loader: This class loader loads classes that are part of the application’s classpath. It is also known as the application class loader. The system class loader is responsible for loading classes from the directories and JAR files specified in the CLASSPATH environment variable.

In addition to these three built-in class loaders, Java also supports custom class loaders that can be created by extending the ClassLoader class. Custom class loaders can be used to load classes from non-standard locations or to provide custom security policies.

The class loader hierarchy in Java is hierarchical, with each class loader delegating the loading of a class to its parent class loader. If the parent class loader cannot find the class, then the child class loader will attempt to load it. This process continues until the class is either found or an error is thrown. This mechanism ensures that classes are loaded only once and that classes loaded by child class loaders have access to the classes loaded by parent class loaders.

Leave a Reply

Your email address will not be published. Required fields are marked *