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.


//from  ww  w  .  j  a  v a  2  s  . c  om
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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable