• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

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

December 29, 2013 //  by Krishna Srinivasan//  Leave a 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)

Category: JavaTag: ClassLoader, CoreJava

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.

Previous Post: « JPA 2 Tutorials
Next Post: JPA Vs Hibernate »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact