Classloaders

This lesson introduces the concept of classloaders in Java.

We'll cover the following...

Question # 1

What is a class loader in Java?

A class loader is responsible for dynamically loading classes into the Java Virtual Machine. Classes are loaded on demand in Java.

Question # 2

How many types of class loaders are there?

There are three types of class loaders:

  • Bootstrap Class Loader: The bootstrap class loader loads the core Java libraries located in the /jre/lib directory. This class loader, which is part of the core JVM, is written in native code.

  • Extensions Class Loader: The extensions class loader loads the code in the extensions directories (/jre/lib/ext, or any other directory specified by the java.ext.dirs system property.

  • System Class Loader: The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable.

We can get the class loader for a class using getClassLoader() method. An example appears below, run the code and examine the output.

Press + to interact
import com.sun.javafx.util.Logging;
class Demonstration {
public static void main( String args[] ) {
System.out.println((new Demonstration()).getClass().getClassLoader());
System.out.println(Logging.class.getClassLoader());
System.out.println("".getClass().getClassLoader());
}
}

If you look at the ...