Class forName() method in Java with Examples (original) (raw)

Last Updated : 12 Jul, 2025

The forName() method of java.lang.Class class is used to get the instance of this Class with the specified class name. This class name is specified as the string parameter.
Syntax:

public static Class forName(String className) throws ClassNotFoundException

Parameter: This method accepts the parameter className which is the Class for which its instance is required.
Return Value: This method returns the instance of this Class with the specified class name.
Exception: This method throws following Exceptions:

Below programs demonstrate the forName() method.
Example 1:

Java `

// Java program to demonstrate forName() method

public class Test { public static void main(String[] args) throws ClassNotFoundException { // get the Class instance using forName method Class c1 = Class.forName("java.lang.String");

    System.out.print("Class represented by c1: "
                     + c1.toString());
}

}

`

Output:

Class represented by c1: class java.lang.String

Example 2:

Java `

// Java program to demonstrate forName() method

public class Test { public static void main(String[] args) throws ClassNotFoundException { // get the Class instance using forName method Class c1 = Class.forName("java.lang.Integer");

    System.out.print("Class represented by c1: "
                     + c1.toString());
}

}

`