Java Class.forName(String name, boolean initialize, ClassLoader loader)

Syntax

Class.forName(String name, boolean initialize, ClassLoader loader) has the following syntax.

public static Class <?> 
           forName(String name,  boolean initialize,   ClassLoader loader)
           throws ClassNotFoundException

Example

In the following code shows how to use Class.forName(String name, boolean initialize, ClassLoader loader) method.


/*w w  w .j av  a 2s  .  com*/
public class Main {

  public static void main(String[] args) throws Exception {

    Class cls = Class.forName("Main");

    ClassLoader cLoader = cls.getClassLoader();

    Class cls2 = Class.forName("java.lang.Thread", true, cLoader);

    System.out.println("Class = " + cls.getName());
    System.out.println("Class = " + cls2.getName());

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable