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


Advertisements


Description

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.

Declaration

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

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

Parameters

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)

Returns

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

Exceptions

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

Example

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

Live Demo
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");
      System.out.println(sampleObject.getSampleField());
   }
}

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 −

data

java_reflect_constructor.htm

Advertisements