A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/classloader-in-java/ below:

ClassLoader in Java - GeeksforGeeks

ClassLoader in Java

Last Updated : 11 Jul, 2025

The Java ClassLoader is an integral part of the Java Runtime Environment (JRE) that dynamically loads Java classes into the Java Virtual Machine (JVM). The Java run time system does not need to know about files and file systems because of classloaders. Java classes aren’t loaded into memory all at once, but when required by an application. At this point, the Java ClassLoader is called by the JRE, and these ClassLoaders load classes into memory dynamically.

ClassLoaders play a crucial role in Java's ability to dynamically load classes into memory as needed, enabling flexibility and efficiency in Java applications.

Types of ClassLoaders in Java

Java's ClassLoaders are categorized into different types, each responsible for loading classes from specific locations:

1. Bootstrap ClassLoader (Primordial ClassLoader): 2. Platform Class Loader (Extension ClassLoader): 3. System ClassLoader (Application ClassLoader): Principles of Functionality of a Java ClassLoader

Java ClassLoaders operate based on specific principles:

1. Delegation Model: 2. Visibility Principle: 3. Uniqueness Property: Methods of java.lang.ClassLoader

Several methods in java.lang.ClassLoader facilitate the loading of classes:

Example:
// Code executed before class loading
Class<?> clazz = ClassLoader.getSystemClassLoader().loadClass("com.example.MyClass");
Java ClassLoader Functionality Principles

Principles of functionality are the set of rules or features on which a Java ClassLoader works. There are three principles of functionality, they are:

  1. Delegation Model: The Java Virtual Machine and the Java ClassLoader use an algorithm called the Delegation Hierarchy Algorithm to Load the classes into the Java file. The ClassLoader works based on a set of operations given by the delegation model. They are:
  2. Visibility Principle: The Visibility Principle states that a class loaded by a parent ClassLoader is visible to the child ClassLoaders but a class loaded by a child ClassLoader is not visible to the parent ClassLoaders. Suppose a class GEEKS.class has been loaded by the Extension ClassLoader, then that class is only visible to the Extension ClassLoader and Application ClassLoader but not to the Bootstrap ClassLoader. If that class is again tried to load using Bootstrap ClassLoader it gives an exception java.lang.ClassNotFoundException.
  3. Uniqueness Property: The Uniqueness Property ensures that the classes are unique and there is no repetition of classes. This also ensures that the classes loaded by parent classloaders are not loaded by the child classloaders. If the parent class loader isn’t able to find the class, only then the current instance would attempt to do so itself.
Key Methods of java.lang.ClassLoader:

After the JVM requests for the class, a few steps are to be followed in order to load a class. The Classes are loaded as per the delegation model but there are a few important Methods or Functions that play a vital role in loading a Class.

  1. loadClass(String name, boolean resolve): This method is used to load the classes which are referenced by the JVM. It takes the name of the class as a parameter. This is of type loadClass(String, boolean).
  2. defineClass(): The defineClass() method is a final method and cannot be overridden. This method is used to define a array of bytes as an instance of class. If the class is invalid then it throws ClassFormatError.
  3. findClass(String name): This method is used to find a specified class. This method only finds but doesn't load the class.
  4. findLoadedClass(String name): This method is used to verify whether the Class referenced by the JVM was previously loaded or not.
  5. Class.forName(String name, boolean initialize, ClassLoader loader): This method is used to load the class as well as initialize the class. This method also gives the option to choose any one of the ClassLoaders. If the ClassLoader parameter is NULL then Bootstrap ClassLoader is used.

Example: The following code is executed before a class is loaded: 

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

Note: If a class has already been loaded, it returns it. Otherwise, it delegates the search for the new class to the parent class loader. If the parent class loader doesn't find the class, loadClass() calls the method findClass() to find and load the class. The findClass() method searches for the class in the current ClassLoader if the class wasn't found by the parent ClassLoader.

This article accurately reflects the updated understanding of Java ClassLoaders, including changes introduced from Java 9 onwards.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4