开发者

Difference between loadClass() and forName()

开发者 https://www.devze.com 2023-01-16 01:09 出处:网络
Does anyone knows what is the different between: Class clazz = getClass().getClassLoader().loadClass(className)开发者_Go百科;

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

精彩评论

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