A class loader dynamically loads classes on demand into the Java Virtual Machine (JVM). Since there is no linking at compile-time in Java, a class loader finds the binary representation of a class instance with a specific name and then creates that class. The ClassLoader
class is an abstraction of a class loader.
Note: To know more about the
ClassLoader
class, refer to the official documentation.
There are three built-in ClassLoader
types in Java, listed in order of priority:
This Answer will focus on the first type—the Bootstrap ClassLoader
.
The Bootstrap ClassLoader
, also known as the Primordial ClassLoader
, is a chunk of machine code that loads the internal classes of the Java Development Kit (JDK), which implements the Java specifications. Unlike other class loaders, this is not a Java class. Instead, it is platform-specific and begins the entire class loading process.
The Bootstrap ClassLoader
also ensures that the basic code to support Java Runtime Environment (JRE) is loaded, including essential classes in the java.util
and java.lang
packages. It loads these classes from the location rt.jar
, which contains all of the compiled class files for the JRE. This is referred to as the Bootstrap classpath and can be represented as such:
Note: To know the differences between JDK, JRE, and JVM, please refer to this Answer.
These are the rules or principles that the class loaders in Bootstrap follow.
The delegation hierarchy model determines the priority of the Bootstrap ClassLoader
, and each type of ClassLoader
, except for the Bootstrap, which has a reference to its parent ClassLoader
. The diagram below illustrates this hierarchy:
According to this principle, the classes loaded by the children class loaders are visible to their parent classes. However, the same is not true the other way around. Therefore, any class loaded by the Bootstrap ClassLoader
will not be visible to the Extension or System class loaders.
This principle ensures that all the loaded classes are unique and there is no repetition. For instance, if the parent class loader loads a class, it will not be loaded again by the child class loader.
Free Resources