Constructor newInstance() method in Java with Examples

27 Nov, 2019
The newInstance() method of a Constructor class is used to create and initialize a new instance of this constructor, with the initialization parameters passed as parameter to this method. Each parameter is unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.
If the number of formal parameters of the constructor is 0, the supplied parameter is of length 0 or null. If the constructor completes normally, returns the newly created and initialized instance.
Syntax:
public T newInstance(Object... initargs)
  throws InstantiationException, IllegalAccessException,
         IllegalArgumentException, InvocationTargetException

Parameters: This method accepts initargs as the parameter which is an array of objects to be passed as arguments to the constructor call. The values of primitive types are wrapped in a wrapper object of the appropriate type (e.g. float in Float).
Return value: This method returns a new object created by calling the constructor this object represents.
Exception: This method throws following Exceptions:
IllegalAccessException: if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
IllegalArgumentException: if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if this constructor pertains to an enum type.
InstantiationException: if the class that declares the underlying constructor represents an abstract class.
InvocationTargetException: if the underlying constructor throws an exception.
ExceptionInInitializerError: if the initialization provoked by this method fails.
Below programs illustrate the newInstance() method:Program 1:
// Java program to demonstrate// Constructor.newInstance() method  import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;  public class GFG {      public static void main(String... args)        throws InstantiationException,               IllegalAccessException,               IllegalArgumentException,               InvocationTargetException    {          // An array of constructor        Constructor[] constructor            = Test.class.getConstructors();          // Apply newInstance method        Test sampleObject            = (Test)constructor[0].newInstance();          System.out.println(sampleObject.value);    }}  class Test {      String value;      public Test()    {        System.out.println("New Instance is created");        value = "New Instance";    }}
Output:
New Instance is created
New Instance

Program 2:
// Java program to demonstrate// Constructor.newInstance() method  import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;  public class GFG {      public static void main(String... args)        throws InstantiationException,               IllegalAccessException,               IllegalArgumentException,               InvocationTargetException    {          // an array of constructor        Constructor[] constructor            = Test.class.getConstructors();          // apply newInstance method        Test sampleObject            = (Test)constructor[0]                  .newInstance("New Field");          System.out.println(sampleObject.getField());    }}  class Test {      private String field;      public Test(String field)    {        this.field = field;    }      public String getField()    {        return field;    }      public void setField(String field)    {        this.field = field;    }}
Output:
New Field

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...)
Java-Constructors
Java-Functions
java-lang-reflect-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.