Difference between Loading a class using ClassLoader and Class.forName

后端 未结 9 1044
梦如初夏
梦如初夏 2020-11-29 20:38

Below are 2 code snippets

The first one uses ClassLoader class to load a specified class

ClassLoader cls = ClassLoader.getSystemClassL

9条回答
  •  情深已故
    2020-11-29 20:57

    ClassLoader.loadClass() uses the specified classloader (the system classloader in your case), whereas Class.forName() uses classloader of the current class.

    Class.forName() can be used when you don't care about particular classloader and want the same classloading behaviour as for statically referenced classes.

提交回复
热议问题