CodeBlanks Logo Icon CodeBlanks

Class loaders

Learn Bootstrap, Platform and Application class loaders.

Class loaders

The class loader finds a class's bytecode from a file and loads it into memory. The JVM uses a three-tier loading model: Bootstrap, Platform and Application loaders.

The Bootstrap loader loads core Java classes (for example java.lang.String) and is written in C++; the getClassLoader() call returns null. The Platform loader loads Java standard libraries; the Application loader loads user classes.

This layered design provides security and modularity; each loader handles its own responsibility.

The getParent chain

Every class loader has a parent loader. The getParent() call returns the parent loader. Application's parent is Platform, Platform's parent is Bootstrap.

ClassLoader app = Main.class.getClassLoader();
ClassLoader plat = app.getParent();
System.out.println(plat);

This chain ensures that a class is first looked up in the parent loader (parent-delegation model). Core classes are thus always loaded from a trusted source.

Practice Print the String class loader

Define a Main class. In main, assign the result of String.class.getClassLoader() to a ClassLoader variable. Print it with System.out.println. (Bootstrap classes return null.)

Starter code

Main.java
public class Main {
    public static void main(String[] args) {
        // Print the loader of the String class
    }
}

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        ClassLoader loader = String.class.getClassLoader();
        System.out.println(loader);
    }
}

Expected output

null

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the Bootstrap loader call

Question
ClassLoader loader = String.class.____();
System.out.println(loader);
Answer
ClassLoader loader = String.class.getClassLoader();
System.out.println(loader);

Explanation: getClassLoader() returns the loader that loaded the class.

2 What does String.class.getClassLoader() return?

  • The Application loader
  • The Platform loader
  • null (Bootstrap loader) Correct answer
  • It throws an error

Explanation: Core classes are loaded by Bootstrap, so it returns null.

3 Match the loader tiers

Term Meaning
Bootstrap Core Java classes
Platform Java standard library
Application User classes
getParent() Parent loader

All lessons in Expert