Java class loader

  • 1.   Explain Java class loader?


Class loader are hierarchical and use a delegation model when loading a class. Here is the “Typical Default Class Loader Hierarchy”.


Java class loader




Class Loader Explanation
Bootstrap Load the core java classes(e.g. java.*, javax.*, etc) into JVM
Extension Load classes from the JRE’s extension directories.
System Load classes from the system class path.


Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true as explained in the above diagram.

Note: Two objects loaded by different classes loaders are never equal even if they carry the same values, which mean a class is uniquely identified in the context of the associated class loader.


  • 2.Explain static & dynamic class loading?


Static class loading knows the class at compile time.

Dynamic class loading knows the class at run time. That lets us maybe change classes through configuration or lets users introduce new classes that we never know about by passing a new string.

// static class loading at compile time

Car c = new Car();

// dynamic class loading at run time.

Class vehicleClass = Class.forName(myClassName) ;


Dynamic class loading methods:

The forName(..) method in class - Class.

The findSystemClass(..) method in class - ClassLoader.

The loadClass(..) method in class - ClassLoader.


你可能感兴趣的:(java,jvm,C++,c,UP)