Difference Between Class.forName() and ClassLoader.loadClass()

SHARE & COMMENT :

Class.forName() and ClassLoader.loadClass(), both the classes dynamically loading the classes to the classpath. However, there are subtle difference on initializing the classes at the time of loading and from where it is loaded. This tutorials compares both the form of class loading with simple examples.

Class.forName()

  • By default the classes are initialized at the time of loading. It means that static variables in the classes are initialized.
  • Also the class is loaded from the current class loader. When you invoke the Class.forName for loading the JDBC driver class, it is loaded to the same class loader from where it is invoked. In short, it is loaded to the caller’s class loader.
  • Class.forName is overloaded method. Invoking with single string parameter is equivalent of Class.forName(className, true, currentLoader). Optionally you can pass the second and third parameters to change the behavior.
    • className – Fully qualified name of the class to be loaded
    • initialize – Whether to initialize the class or not. By default the value is “true”
    • classLoader – By default the value is current class loader. Optionally you can change the class loader name.

ClassLoader.loadClass()

  • By default, the classes are not initialized. The classes are loaded and made available in the classpath, the variables are initialized only when it is first time invoked by the caller.
  • Another advantage of this class is that you can load the classes to any specific class loader. Which may or may not be the loader that loads that calling code. If picking a specific loader to load the class is important to your design, you should ClassLoader.loadClass().

Class Loading Example

TestClass.java

package javabeat.net.corejava;
public class TestClass {
	static {
		System.out.println("Static Initializer Called!!");
		}
}

ClassLoadingExample.java

package javabeat.net.corejava;
public class ClassLoadingExample {
public static void main(String[] args) {
	try {
	System.out.println("Before Loading the forName");
	Class.forName("javabeat.net.corejava.TestClass");
	System.out.println("After Loading the forName");
        ClassLoader.getSystemClassLoader().loadClass("javabeat.net.corejava.TestClass");
	System.out.println("After Loading the loadClass");
	}catch (ClassNotFoundException e){
		e.printStackTrace();
	}
   }

}

Output

Before Loading the forName
Static Initializer Called!!
After Loading the forName
After Loading the loadClass

ClassNotFoundException

If the class is not found in the classpath, you would encounter the following exception.

java.lang.ClassNotFoundException: javabeat.net.xml.TestClass1
	at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:188)
	at javabeat.net.corejava.ClassLoadingExample.main(ClassLoadingExample.java:8)

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. There is one visible difference is that, where Class.forName(String className) method is static method of java.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.

    Here you can see more differences with really good blog on this topic

    Click here http://www.somanyword.com/2013/12/difference-between-class-forname-and-classloader-loadclass-methods-in-java/

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better