Java Reflection - Java Class.forName(String className)


Class.forName(String className) has the following syntax.

public static Class <?> forName(String className)   throws ClassNotFoundException


In the following code shows how to use Class.forName(String className) method.

public class Main {
/* w  w w.  j  a  m*/
  public static void main(String[] args) throws Exception {

    // returns the Class object for the class with the specified name
    Class cls = Class.forName("java.lang.ClassLoader");

    // returns the name and package of the class
    System.out.println("Class found = " + cls.getName());
    System.out.println("Package = " + cls.getPackage());


The code above generates the following result.