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
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<Test> constructor = Test.class.getConstructor();
constructor.newInstance();
}
}
Output
Object created
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
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
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;
}
}
Output
New 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.