How do I create an object via its multiparameter constructor using reflection?
Author: Deron Eriksson
Description: This Java tutorial describes how to create an object by calling its multi-parameter constructor via reflection.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.4


In JavaSW, if you have a Class object, it is possible to obtain a particular Constructor object for that class by calling getConstructor() on that Class object. To specify the particular parameter types of the constructor, you can do so by passing a Class array of classes as the parameter to the getConstructor() call. Once you have the particular constructor, you can instantiate an object by calling newInstance() on the Constructor object. You can pass in the various parameters to the constructor via an Object array. This may sounds confusing, but it's actually not very complicated to understand if you see an example.

The ClassConstructorTest class below shows this. As a first example, it shows how you can instantiate an object via a no-argument constructor by calling newInstance() on a Class object. Following that, it gives another example, but this time it obtains a Constructor object from a Class object by calling getConstructor() on the Class object, with a Class array parameter specifying the parameter types of the particular constructor. Following that, it instantiates an object of the particular class by calling the newInstance() method on the Constructor object, with an Object array parameter specifying the parameters to pass to the constructor.

ClassConstructorTest.java

package test;

import java.lang.reflect.Constructor;

public class ClassConstructorTest {

	public static void main(String args[]) throws Throwable {
		Class ctClass = ConstructorTest.class;

		// creating an object calling no argument constructor via newInstance of Class object
		ConstructorTest ctNoArgs = (ConstructorTest) ctClass.newInstance();
		ctNoArgs.setPub("created this with ctClass.newInstance()");
		System.out.println("pub:" + ctNoArgs.getPub());

		// creating an object by getting Constructor object (with parameters) and calling newInstance (with parameters) on it
		Constructor constructor = ctClass.getConstructor(new Class[] { String.class, String.class, String.class });
		ConstructorTest ctArgs = (ConstructorTest) constructor.newInstance(new Object[] { "first", "second", "third" });
		ctArgs.setPub("created this with constructor.newInstance(new Object[] { \"first\", \"second\", \"third\" })");

		System.out.println("\npub:" + ctArgs.getPub());
		System.out.println("pro:" + ctArgs.getPro());
		System.out.println("pri:" + ctArgs.getPri());
	}
}

ConstructorTest is shown below:

ConstructorTest.java

package test;

public class ConstructorTest {

	private String pri;
	protected String pro;
	public String pub;

	public ConstructorTest() {
	}

	public ConstructorTest(String pri, String pro, String pub) {
		this.pri = pri;
		this.pro = pro;
		this.pub = pub;
	}

	public String getPri() {
		return pri;
	}

	public void setPri(String pri) {
		this.pri = pri;
	}

	public String getPro() {
		return pro;
	}

	public void setPro(String pro) {
		this.pro = pro;
	}

	public String getPub() {
		return pub;
	}

	public void setPub(String pub) {
		this.pub = pub;
	}

}

Executing ClassConstructorTest yields the following results:

pub:created this with ctClass.newInstance()

pub:created this with constructor.newInstance(new Object[] { "first", "second", "third" })
pro:second
pri:first

If you have a Class object and need to instantiate an object of that class via its no-argument constructor, you can do so by calling newInstance() on the Class object. If you need to instantiate an object of a class via a Class object and you need to use a constructor that has parameters, you can do so by calling getConstructor(Class []) on the Class object to get a Constructor object, and then you can call newInstance(Object []) on the Constructor object to instantiate the object.