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
try to dynamically load class with given string class name and both methods return
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
in other hand,
instance method (non-static method). Therefore, if you want to use and call
to load any class with this method.
One another difference is
given class. Where
same
For example, if you have created a class XYZ in which you have used
class has loaded. Whereas
use that
this method is called. If you are concerned about any specific or your own
load any class then you should use
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.
using
But there is one more method in
can specify your own
initialization as well. There is one method with three parameters in which you can specify your
set
For example,
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.
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.Classobjects
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.Classclass,
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.ClassLoaderinstance
to load any class with this method.
One another difference is
ClassLoaderused by both methods to load
given class. Where
Class.forName(String className)method use the
same
ClassLoaderto 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
ClassLoaderwith which XYZ
class has loaded. Whereas
ClassLoader.LoadClass(String className)method
use that
ClassLoaderon which
ClassLoaderinstance
this method is called. If you are concerned about any specific or your own
ClassLoaderto
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.Classclass, in which you
can specify your own
ClassLoaderto load any class and delay class
initialization as well. There is one method with three parameters in which you can specify your
ClassLoaderand
set
Booleanflag, 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.
相关文章推荐
- Difference Between Class.forName() and ClassLoader.loadClass()
- Difference between Class.forName() and ClassLoader.loadClass()
- The Difference of Class.forName()、Class.forName().newInstance() and New in Java
- @RequestParam注解使用:Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
- Class.forName() and ClassLoader.loadClass()
- difference between Abstract class and interface in java
- What’s the difference between an interface and an abstract class in Java?
- Java中class是如何加载到JVM中的(Class.forName("name")和ClassLoader.loadClass("name")的区别)
- Java中class是如何加载到JVM中的(Class.forName("name")和ClassLoader.loadClass("name")的区别)
- difference between Abstract class and interface in java
- Java:Class.forName()与ClassLoader.loadClass()的区别
- difference between Interface and abstract class in Java
- Difference between Abstract Class and Interface in Java
- java两种反射的区别 - Class.forName()和ClassLoader.loadClass()
- Relationship between java file name and the declared class name in it
- JAVA的Class.forName(clsName)ClassLoader的loadClass(clsName)的区别
- NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java class
- What is difference between HashMap and Hashtable in Java?
- Class.forname() 和 ClassLoader.loadClass()的区别
- There is no getter for property named 'name' in 'class java.lang.Integer'