The java.lang.Class<T> class can be used to instantiate Java objects without calling explicitly their class constructor. For example, we can load a class by specifying its fully qualified name (i.e. the class name preceded by the name of the package in which the class is defined), using the forName(String className) static method of Class. If the given class can be found by the class loader, this method returns a Class object corresponding to the given class, otherwise it throws a java.lang.ClassNotFoundException. To instantiate an object of the returned class, we can use the newInstance() method, which returns an object created with the default constructor, i.e. the no-argument constructor. If the class or its constructor are not accessible form the calling code, a java.lang.IllegalAccessException is thrown, and if the instantiation fails for other reasons (for example, if the class does not have a no-argument constructor), a java.lang.InstantiationException is thrown.

 The following example illustrates the usage of these methods to create a java.util.ArrayList object.

import java.util.ArrayList;

public class NoArgumentConstructor {
  public static void main(String str[]){
    try{
      ArrayList list = (ArrayList)  
        (Class.forName("java.util.ArrayList").newInstance());
      System.out.println("The no-argument constructor exists.");
      System.out.println("ArrayList object created successfully.");
    } catch (Exception e) {
      System.out.println(
        "The no-argument constructor does not exist.");
      System.out.println(e.getMessage());
    }
  }
}

 The output of this program, reported below, indicates that the ArrayList class has a no-argument constructor and can be instantiated.

The no-argument constructor exists.

ArrayList object created successfully.