欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java class loader 博客分类: JDK JavaJVMUPCC++

程序员文章站 2024-02-27 22:16:57
...
  • 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
            
    
    博客分类: JDK JavaJVMUPCC++




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 class loader
            
    
    博客分类: JDK JavaJVMUPCC++
  • 大小: 10.3 KB