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 ofjava.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 whichClassLoader 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
usingClassLoader.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.
本文对比了Java中Class.forName和ClassLoader.loadClass两种动态加载类的方法。前者为静态方法且加载时初始化静态成员;后者为实例方法,允许指定类加载器并延迟类的初始化。
699

被折叠的 条评论
为什么被折叠?



