Difference Between Class.ForName() And ClassLoader.LoadClass() Methods In Java
来源:互联网 发布:域名做别名解析 编辑:程序博客网 时间:2024/05/21 07:03
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.
- Difference Between Class.ForName() And ClassLoader.LoadClass() Methods In Java
- Difference between Class.forName() and ClassLoader.loadClass()
- Difference Between Class.forName() and ClassLoader.loadClass()
- Class.forName() and ClassLoader.loadClass()
- Class.forName()、ClassLoader.loadClass()
- Class.forName VS ClassLoader.loadClass
- Java:Class.forName()与ClassLoader.loadClass()的区别
- Java中Class.forName()和ClassLoader.loadClass()的区别
- Java中Class.forName和ClassLoader.loadClass的区别
- Java中Class.forName和ClassLoader.loadClass的区别
- Java中Class.forName和ClassLoader.loadClass的区别
- The Difference of Class.forName()、Class.forName().newInstance() and New in Java
- difference between Interface and abstract class in Java
- difference between Abstract class and interface in java
- difference between Abstract class and interface in java
- Difference between Abstract Class and Interface in Java
- Class.forName()与ClassLoader.loadClass()的区别
- Class.forName和ClassLoader.loadClass的比较
- Struts2 标签判断和获取 request
- 黑马程序员_变量命名
- 我的2013
- 64位 Kubuntu\Ubuntu 下使用pywebqq
- Fedora 20 禁止自动下载更新
- Difference Between Class.ForName() And ClassLoader.LoadClass() Methods In Java
- 【黑马训练营】优化大数据插入
- epoll在多进程下产生的“惊群”现象
- 用最简单的方式整合JFreechart和Structs并修改背景色
- sicily 1509. Rails
- Fedora19中使用maven的一个错误:UnsupportedClassVersionError
- socket编程——局域网聊天
- 关于OpenGL的RenderBuffer,FrameBuffer
- 个人android学习笔记