java.lang.reflect.Constructor.newInstance() Method Example



The java.lang.reflect.Constructor.newInstance(Object... initargs) method uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.


Following is the declaration for java.lang.reflect.Constructor.newInstance(Object... initargs) method.

public T newInstance(Object... initargs) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException


initargs − array of objects to be passed as arguments to the constructor call; values of primitive types are wrapped in a wrapper object of the appropriate type (e.g. a float in a Float)


a new object created by calling the constructor this object represents.


  • IllegalAccessException − if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.

  • IllegalArgumentException − if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if this constructor pertains to an enum type.

  • InstantiationException − if the class that declares the underlying constructor represents an abstract class.

  • InvocationTargetException − if the underlying constructor throws an exception.

  • ExceptionInInitializerError − if the initialization provoked by this method fails.


The following example shows the usage of java.lang.reflect.Constructor.newInstance(Object... initargs) method.

package com.tutorialspoint;

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

public class ConstructorDemo {
   public static void main(String[] args) throws NoSuchMethodException, 
         SecurityException, InstantiationException, IllegalAccessException, 
         IllegalArgumentException, InvocationTargetException {
      Constructor constructor = SampleClass.class.getConstructor(String.class);
      SampleClass sampleObject = (SampleClass)constructor.newInstance("data");

class SampleClass {
   private String sampleField;
   public SampleClass(){

   public SampleClass(String sampleField){
      this.sampleField = sampleField;

   public String getSampleField() {
      return sampleField;

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField;

Let us compile and run the above program, this will produce the following result −