您的位置:首页 > Web前端

Difference Between Class.ForName() And ClassLoader.LoadClass() Methods In Java

2014-01-01 18:58 621 查看
This is one of the most frequently asked questions in Java interviews. This question describes how deeply you know about class loading, dynamic class loading mechanism and class initialization. Both 
Class.forName(String
className)
 and 
ClassLoader.LoadClass(String className)
 methods
try to dynamically load class with given string class name and both methods return 
java.lang.Class
 objects
for given string class name.

But now question arise that if both methods have same behavior than what is the difference between them? There is one visible difference is that, where 
Class.forName(String
className)
 method is static method of
java.lang.Class
 class,
in other hand, 
ClassLoader.LoadClass(String className)
 method is
instance method (non-static method). Therefore, if you want to use and call 
ClassLoader.LoadClass(String
className)
method, you need any 
java.lang.ClassLoader
 instance
to load any class with this method.

One another difference is 
ClassLoader
 used by both methods to load
given class. Where 
Class.forName(String className)
 method use the
same 
ClassLoader
 to load the class, which is used by it’s caller.
For example, if you have created a class XYZ in which you have used 
Class.forName(String
className)
 method to load any given class, in this case, 
Class.forName(String
className)
 method will use same 
ClassLoader
 with which XYZ
class has loaded. Whereas 
ClassLoader.LoadClass(String className)
 method
use that 
ClassLoader
 on which
ClassLoader
 instance
this method is called. If you are concerned about any specific or your own 
ClassLoader
 to
load any class then you should use 
ClassLoader.LoadClass(String className)
 method
to load any given class.

Let us discuss one major difference between both methods. The major difference between both methods is class initialization after loading any given class. 
Class.forName(String
className)
 method load given class dynamically and initialize it statically. It initialize all static fields of this class and it’s super class static fields recursively. Whereas 
ClassLoader.LoadClass(String
className)
 method load the class dynamically but delay the initialization of given class. Therefore, choice of use from both methods depends on situations, if you know, static initialization of any class is costly, then you could choose delay initialization
using
ClassLoader.LoadClass(String className)
 method.

But there is one more method in 
java.lang.Class 
class, in which you
can specify your own 
ClassLoader
 to load any class and delay class
initialization as well. There is one method with three parameters in which you can specify your 
ClassLoader
 and
set 
Boolean
 flag, whether you want to initialize that class or not.
For example,
Class.forName(String className, boolean initialize, ClassLoader
loader)
. This method can be use for more flexibility and customization like initialization and 
ClassLoader
.

I hope now you are bit more clear about differences in both methods which generally look alike. If you know any more differences please share with us.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐