Java Reflection - Java Class.newInstance()








Syntax

Class.newInstance() has the following syntax.

public T newInstance()  throws InstantiationException ,   IllegalAccessException

Example

In the following code shows how to use Class.newInstance() method.

//  www.ja va  2 s . c  o m
import java.util.Date;

public class Main {

  public static void main(String[] args) throws Exception {
    Date d = new Date();
    Class cls = d.getClass();
    System.out.println("Time = " + d.toString());


    Object obj = cls.newInstance();
    System.out.println("Time = " + obj);
  }
}

The code above generates the following result.