Class forName() method in Java with Examples
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.
public static Class<T> 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:
- LinkageError: if the linkage fails
- ExceptionInInitializerError: if the initialization provoked by this method fails
- ClassNotFoundException: if the class cannot be located
Below programs demonstrate the forName() method.
Class represented by c1: class java.lang.String
Class represented by c1: class java.lang.Integer