1

As I found in one thread Java: Difference between Class.forName and ClassLoader.loadClass that Class.forName() will always use the ClassLoader of the caller, whereas ClassLoader.loadClass() can specify a different ClassLoader

System.out.println(Class.forName("test.Employee").getClassLoader());
System.out.println(ClassLoader.getSystemClassLoader().loadClass("test.Employee").getClassLoader());

But above two lines are printing the same result

sun.misc.Launcher$AppClassLoader@7ced01
sun.misc.Launcher$AppClassLoader@7ced01

Please explain the reason.

Community
  • 1
  • 1
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30

1 Answers1

0

There is no difference for you as your current class was loaded using the SystemClassLoader wich happens to be sun.misc.Launcher$AppClassLoader@7ced01.

If you use a different classloader to load a class, both aproaches will use different classloaders when used in that class you loaded with a different classloader.

TheConstructor
  • 4,285
  • 1
  • 31
  • 52