loadClass() Method - Loading Classes Explicitly

This section provides a tutorial example on how to load classes into JVM in 3 ways: loadClass() method, Class.forName() method, and 'new' operator.

There are several ways to load a Java class into the JVM:

1. Using the "new" operator: When you use the "new" operator and a class constructor to create a new instance of a Java class, JVM will use the default ClassLoader to load the class. This the most common way of loading a Java class. For example:

Object o = new Hello();
Class c = o.getClass();

2. Using the Class.forName() method: When you use the Class.forName() method, JVM will use the default ClassLoader or the specified ClassLoader to the load the specified class. For example:

Class c1 = Class.forName("ClassLoaderTest");
Class c2 = Class.forName("Hello", true, currentLoader);

3. Using the ClassLoader.loadClass() method: When you use the Class.forName() method, JVM will use the given ClassLoader to the load the specified class. For example:

Class c3 = currentLoader.loadClass("SystemInOut");

Here is tutorial example program showing you how to load a Java class in different ways:

/**
 * ClassLoaderLoadClass.java
 * Copyright (c) 2010, HerongYang.com, All Rights Reserved.
 */
class ClassLoaderLoadClass {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      Object o = null;
      Class c = null;
      ClassLoader l = null;

      out.println("");
      out.println("Loading with 'new' operator...");
      o = new Hello();
      c = o.getClass();

      out.println("");
      out.println("Loading with Class.forName() method...");
      try {
         c = Class.forName("ClassLoaderTest");
      } catch (Exception e) {
         e.printStackTrace();
      }

      out.println("");
      out.println("Loading with ClassLoader.loadClass() method...");
      l = ClassLoader.getSystemClassLoader();
      try {
         c = l.loadClass("SystemInOut");
      } catch (Exception e) {
         e.printStackTrace();
      }

      out.println("");
      out.println("Loading the same class again...");
      l = ClassLoader.getSystemClassLoader();
      try {
         c = l.loadClass("SystemInOut");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>javac Hello.java
C:\herong\jvm>javac SystemInOut.java
C:\herong\jvm>javac ClassLoaderTest.java

C:\herong\jvm>javac ClassLoaderLoadClass.java

C:\herong\jvm>java -verbose:class ClassLoaderLoadClass
...
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded ClassLoaderLoadClass from file:/C:/herong/jvm/]

Loading with 'new' operator...
[Loaded Hello from file:/C:/herong/jvm/]

Loading with Class.forName() method...
[Loaded ClassLoaderTest from file:/C:/herong/jvm/]

Loading with ClassLoader.loadClass() method...
[Loaded SystemInOut from file:/C:/herong/jvm/]

Loading the same class again...
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]

The '-verbose:class' output confirms that all 3 ways of loading classes are working. If you trying to load the same class again, the JVM will ignore your request.

Comparing to the same test with JDK 1.6.0 in 2010, the behavior of class loading is not changed.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

ClassLoader Class - Class Loaders

 What Is Class Loader?

 What Is java.lang.ClassLoader Class?

 Accessing the ClassLoader of a Class

 JVM "-verbose:class" Option

loadClass() Method - Loading Classes Explicitly

 getSystemResource() Method - Finding Files

 Class Loading Problem - JAR Hell

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 Outdated Tutorials

 References

 PDF Printing Version