Java class loader

[list]
[*]1. Explain Java class loader?
[/list]

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


[img]http://dl.iteye.com/upload/attachment/162331/4f97917a-8cb7-3217-a72c-abb7c4bed9f1.jpg[/img]




[table]
||[b]Class Loader[/b]||[b]Explanation[/b]
||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.
[/table]

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.


[list]
[*]2.Explain static & dynamic class loading?
[/list]

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) ;


[b]Dynamic class loading methods:[/b]

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

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

The loadClass(..) method in class - ClassLoader.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值