Java Class newInstance() Method



Description

The Java Class newInstance() method creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized. This method is deprecated since Java 9.

Declaration

Following is the declaration for java.lang.Class.newInstance() method

public T newInstance() throws InstantiationException, IllegalAccessException

Parameters

NA

Return Value

This method returns a newly allocated instance of the class represented by this object.

Exception

  • IllegalAccessException − if the class or its nullary constructor is not accessible.

  • InstantiationException − If this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.

  • ExceptionInInitializerError − If the initialization provoked by this method fails.

  • SecurityException − If a security manager, s, is present.

Creating a new Instance of a Class Example

The following example shows the usage of java.lang.Class.newInstance() method. In this example, we've created a Date Object and then using getClass() method, we've retrieved the class of the date instance. Now using newInstance() method, a new instance of the class is created and printed.

package com.tutorialspoint;

import java.util.Date;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         // date object
         Date d = new Date();
         Class cls = d.getClass();
         System.out.println("Time = " + d.toString());

         /* creates a new instance of the class represented by this 
            Class object cls */
         Object obj = cls.newInstance();
         System.out.println("Time = " + obj);
      } catch(InstantiationException e) {
         System.out.println(e.toString());
      } catch(IllegalAccessException e) {
         System.out.println(e.toString());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Time = Tue Oct 23 16:09:55 MST 2012
Time = Tue Oct 23 16:09:55 MST 2012

Creating a new Instance of an ArrayList Example

The following example shows the usage of java.lang.Class.newInstance() method. In this example, we've used class of ArrayList. Now using newInstance() method, a new instance of the class is created and printed.

package com.tutorialspoint;

import java.util.ArrayList;

public class ClassDemo {

   public static void main(String[] args) {

      try {

         Class cls = ArrayList.class;

         /* creates a new instance of the class represented by this 
            Class object cls */
         Object obj = cls.newInstance();
         System.out.println("ArrayList = " + obj);
      } catch(InstantiationException e) {
         System.out.println(e.toString());
      } catch(IllegalAccessException e) {
         System.out.println(e.toString());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = []

Creating a new Instance of a Thread Example

The following example shows the usage of java.lang.Class.newInstance() method. In this example, we've used class of Thread. Now using newInstance() method, a new instance of the class is created and printed.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {

      try {

         Class cls = Thread.class;

         /* creates a new instance of the class represented by this 
            Class object cls */
         Object obj = cls.newInstance();
         System.out.println("Thread = " + obj);
      } catch(InstantiationException e) {
         System.out.println(e.toString());
      } catch(IllegalAccessException e) {
         System.out.println(e.toString());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Thread = Thread[#21,Thread-0,5,main]
java_lang_class.htm
Advertisements