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.
- Supports both no-argument and parameterized constructors
- Accepts constructor arguments dynamically
- Performs automatic unboxing for primitive parameters
- Returns a fully initialized object
- Commonly used in reflection-based frameworks for dynamic object creation Java `
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:
- **Test.class.getConstructors(): retrieves all public constructors of the Test class.
- **constructor[0].newInstance(): invokes the no-argument constructor reflectively, creating a new Test object and executing its constructor body, which initializes the value field and prints the message.
**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:
- The getConstructors() method retrieves all public constructors of the Test class at runtime.
- The first constructor is selected and invoked using newInstance("New Field"), passing a String argument dynamically.
- The parameterized constructor of Test initializes the field variable with the provided value.
- The getField() method is called on the created object to verify successful object creation and initialization.