VOOZH about

URL: https://www.geeksforgeeks.org/java/constructor-newinstance-method-in-java-with-examples/

⇱ Constructor newInstance() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Constructor newInstance() method in Java with Examples

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

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

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


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.
Comment