开发者

Difference between loadClass() and forName()

Does anyone knows what is the different between:

Class clazz = getClass().getClassLoader().loadClass(className)开发者_Go百科;

AND

Class clazz = Class.forName(className);

As i tried both, it gave me same result.


Class.forName(className) execute the static initializer code blocks in the loaded class.

A call to forName("X") causes the class named X to be initialized.

getClass().getClassLoader().loadClass(className) doesn't.


Class.forName(className, false, getClass().getClassLoader()) is the same as getClass().getClassLoader().loadClass(className).


Resources :

  • javadoc - Class.forName()
  • Javadoc - ClassLoader.loadClass()
  • JLS - Initialization of Classes and Interfaces
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜