WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Core Java · Advanced · question 45 of 100

What is a ClassLoader in Java? How does it work?

📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

In Java, a ClassLoader is responsible for loading classes into the JVM (Java Virtual Machine) at runtime. The ClassLoader loads classes from various sources, such as local file systems, network locations, or even from memory.

The ClassLoader is an important part of the Java runtime environment, as it enables dynamic class loading and allows Java programs to be more flexible and modular.

The ClassLoader in Java works by following a hierarchical structure. When a Java program requests a class, the ClassLoader first looks for the class in the bootstrap class loader, which is responsible for loading core Java classes from the Java runtime environment.

If the class is not found in the bootstrap class loader, the ClassLoader then looks for the class in the extension class loader, which is responsible for loading Java extension classes.

If the class is still not found, the ClassLoader then looks for the class in the system class loader, which is responsible for loading application classes.

If the class is not found by any of these class loaders, the ClassLoader then throws a ClassNotFoundException.

Here is an example of how a ClassLoader can be used to load a class at runtime:

public class MyClass {
    public static void main(String[] args) {
        try {
            // create a new instance of the custom class loader
            ClassLoader classLoader = new MyClassLoader();

            // load the class using the custom class loader
            Class<?> myClass = classLoader.loadClass("MyClass");
    
            // create a new instance of the loaded class
            Object obj = myClass.newInstance();
    
            // call a method on the loaded class
            Method method = myClass.getMethod("myMethod");
            method.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class MyClassLoader extends ClassLoader {
    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        // load the class from a file or other source
        byte[] bytes = loadClassBytes(name);
    
        // define the class using the loaded bytes
        return defineClass(name, bytes, 0, bytes.length);
    }
    
    private byte[] loadClassBytes(String name) {
        // load the class bytes from a file or other source
        // return the bytes as a byte array
    }
}

In this example, a custom ClassLoader called MyClassLoader is created and used to load the MyClass class at runtime. The findClass() method is overridden in the custom ClassLoader to load the class bytes from a file or other source, and the defineClass() method is used to define the class using the loaded bytes.

Once the class is loaded, an instance of the class is created using the newInstance() method, and a method on the class is called using reflection.

In summary, the ClassLoader in Java is responsible for loading classes into the JVM at runtime, and works by following a hierarchical structure. Custom ClassLoaders can also be created to load classes from custom sources, enabling more flexible and modular Java programs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Core Java interview — then scores it.
📞 Practice Core Java — free 15 min
📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

All 100 Core Java questions · All topics