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) 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("LongSleep");
      } catch (Exception e) {
         e.printStackTrace();
      }

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

Prepare some other classes to support the above program:

herong> javac Hello.java
herong> javac SystemInOut.java
herong> javac LongSleep.java

Run the above program with HotSpot JVM:

herong> java -verbose:class ClassLoaderLoadClass

Loading with 'new' operator...
[0.359s][info][class,load] Hello source: file:/C:/herong/ClassLoader/

Loading with Class.forName() method...
[0.361s][info][class,load] ClassLoaderTest source:
   file:/C:/herong/ClassLoader/

Loading with ClassLoader.loadClass() method...
[0.362s][info][class,load] LongSleep source: file:/C:/herong/ClassLoader/

Loading the same class again...

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.

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

 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

 ClassChecker.java - Reports Class Loader

 ClassChecker.java - Reports Class Locations

 "superclass access check failed" Class Load Error

 Class Loading Followed by Class Initialization

 Class Class - Class Reflections

 JVM Runtime Data Areas

 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

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

 Archived Tutorials

 References

 Full Version in PDF/EPUB