Open In App

Constructor newInstance() method in Java with Examples

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:

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…)


Article Tags :