Constructor newInstance() method in Java with Examples (original) (raw)

Last Updated : 21 Jan, 2026

The newInstance() method of the java.lang.reflect.Constructor class is used to dynamically create objects at runtime by invoking a specific constructor through Java Reflection.

import java.lang.reflect.Constructor; class Test{ public Test(){ System.out.println("Object created"); } } public class GFG { public static void main(String[] args) throws Exception { Constructor constructor = Test.class.getConstructor(); constructor.newInstance(); } }

`

**Explanation: The constructor is obtained using reflection and invoked using newInstance() to create a new object at runtime without using the new keyword.

**Syntax:

constructor.newInstance(arguments);

****Return Type:**T - a new instance created by invoking the constructor

**Example 1: Creating an Object Using a No-Argument Constructor

Java `

import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException;

public class GFG{ public static void main(String... args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Constructor[] constructor= Test.class.getConstructors(); 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

**Explanation:

**Example 2: Creating an Object Using a Parameterized Constructor

Java `

import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException;

public class GFG{ public static void main(String... args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Constructor[] constructor= Test.class.getConstructors(); 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;
}

}

`

**Explanation: