Reflection – Constructors

In today’s tutorial we are going to talk about java reflection constructor mechanisms. We are going to show you how to get a list of constructors by calling getConstructors() and getDeclaredConstructors() methods of the java.lang.Class. Take a look at the following example.

ClassWithSeveralPublicConstructors

package com.itcuties.java.reflection.constructors.data;

public class ClassWithSeveralPublicConstructors {
	public ClassWithSeveralPublicConstructors() {
		System.out.println(ClassWithSeveralPublicConstructors.class.getName() + ".ClassWithSeveralConstructors()");
	}
	
	public ClassWithSeveralPublicConstructors(int param1) {
		System.out.println(ClassWithSeveralPublicConstructors.class.getName() + ".ClassWithSeveralConstructors(int)");
	}
	
	public ClassWithSeveralPublicConstructors(Object param1) {
		System.out.println(ClassWithSeveralPublicConstructors.class.getName() + ".ClassWithSeveralConstructors(Object)");
	}
	
	public ClassWithSeveralPublicConstructors(boolean booleanParam, byte byteParam, short shortParam, int intParam, long longParam) {
		System.out.println(ClassWithSeveralPublicConstructors.class.getName() + ".ClassWithSeveralConstructors([with primitves])");
	}
	
	public ClassWithSeveralPublicConstructors(Boolean booleanParam, Byte byteParam, Short shortParam, Integer intParam, Long longParam) {
		System.out.println(ClassWithSeveralPublicConstructors.class.getName() + ".ClassWithSeveralConstructors([with objects])");
	}
	
	@Override
	public String toString() {
		return "*" + this.getClass().getName() + "*";
	}
}

We are going to list all the constructors by calling getConstructors() method like this.

	System.out.println("Enlisting public constructors of the class.");
	for(Constructor<?> constructor : ClassWithSeveralPublicConstructors.class.getConstructors()) {
		System.out.println("\t\t" + constructor);
	}

The output is:

    Enlisting public constructors of the class.
		public com.itcuties.java.reflection.constructors.data.ClassWithSeveralPublicConstructors()
		public com.itcuties.java.reflection.constructors.data.ClassWithSeveralPublicConstructors(int)
		public com.itcuties.java.reflection.constructors.data.ClassWithSeveralPublicConstructors(java.lang.Object)
		public com.itcuties.java.reflection.constructors.data.ClassWithSeveralPublicConstructors(boolean,byte,short,int,long)
		public com.itcuties.java.reflection.constructors.data.ClassWithSeveralPublicConstructors(java.lang.Boolean,java.lang.Byte,java.lang.Short,java.lang.Integer,java.lang.Long)

As you can see there are all the constructors listed because they are all public.

Let’s take another example. This class contains only constructors that are private

ClassWithSeveralPrivateConstructors

package com.itcuties.java.reflection.constructors.data;

public class ClassWithSeveralPrivateConstructors {
	private ClassWithSeveralPrivateConstructors() {
		System.out.println(ClassWithSeveralPrivateConstructors.class.getName() + ".ClassWithSeveralConstructors()");
	}
	
	private ClassWithSeveralPrivateConstructors(int param1) {
		System.out.println(ClassWithSeveralPrivateConstructors.class.getName() + ".ClassWithSeveralConstructors(int)");
	}
	
	private ClassWithSeveralPrivateConstructors(Object param1) {
		System.out.println(ClassWithSeveralPrivateConstructors.class.getName() + ".ClassWithSeveralConstructors(Object)");
	}
	
	private ClassWithSeveralPrivateConstructors(boolean booleanParam, byte byteParam, short shortParam, int intParam, long longParam) {
		System.out.println(ClassWithSeveralPrivateConstructors.class.getName() + ".ClassWithSeveralConstructors([with primitves])");
	}
	
	private ClassWithSeveralPrivateConstructors(Boolean booleanParam, Byte byteParam, Short shortParam, Integer intParam, Long longParam) {
		System.out.println(ClassWithSeveralPrivateConstructors.class.getName() + ".ClassWithSeveralConstructors([with objects])");
	}
	
	@Override
	public String toString() {
		return "*" + this.getClass().getName() + "*";
	}
}

To list those constructors we need to use getDeclaredConstructors() method.

    System.out.println("Enlisting all constructors of the class.");
	for(Constructor<?> constructor : ClassWithSeveralPrivateConstructors.class.getDeclaredConstructors()) {
		System.out.println("\t\t" + constructor);
	}

The output is:

    Enlisting all constructors of the class.
		private com.itcuties.java.reflection.constructors.data.ClassWithSeveralPrivateConstructors()
		private com.itcuties.java.reflection.constructors.data.ClassWithSeveralPrivateConstructors(int)
		private com.itcuties.java.reflection.constructors.data.ClassWithSeveralPrivateConstructors(java.lang.Object)
		private com.itcuties.java.reflection.constructors.data.ClassWithSeveralPrivateConstructors(boolean,byte,short,int,long)
		private com.itcuties.java.reflection.constructors.data.ClassWithSeveralPrivateConstructors(java.lang.Boolean,java.lang.Byte,java.lang.Short,java.lang.Integer,java.lang.Long)

Next example shows a class that has constructors with all the access modifiers.

ClassWithSeveralVariousAccessConstructors

package com.itcuties.java.reflection.constructors.data;

public class ClassWithSeveralVariousAccessConstructors {
	private ClassWithSeveralVariousAccessConstructors() {
		System.out.println(ClassWithSeveralVariousAccessConstructors.class.getName() + ".ClassWithSeveralConstructors()");
	}
	
	@SuppressWarnings("all")
	public ClassWithSeveralVariousAccessConstructors(int param1) throws Exception {
		System.out.println(ClassWithSeveralVariousAccessConstructors.class.getName() + ".ClassWithSeveralConstructors(int)");
	}
	
	/*package*/ ClassWithSeveralVariousAccessConstructors(Object param1) {
		System.out.println(ClassWithSeveralVariousAccessConstructors.class.getName() + ".ClassWithSeveralConstructors(Object)");
	}
	
	protected ClassWithSeveralVariousAccessConstructors(boolean booleanParam, byte byteParam, short shortParam, int intParam, long longParam) {
		System.out.println(ClassWithSeveralVariousAccessConstructors.class.getName() + ".ClassWithSeveralConstructors([with primitves])");
	}
	
	@Override
	public String toString() {
		return "*" + this.getClass().getName() + "*";
	}
}

To list them all we need to call getDeclaredConstructors() method.

    System.out.println("Enlisting all constructors of the class.");
	for(Constructor<?> constructor : ClassWithSeveralVariousAccessConstructors.class.getDeclaredConstructors()) {
		System.out.println("\t\t" + constructor);
	}

The output is:

    Enlisting all constructors of the class.
		private com.itcuties.java.reflection.constructors.data.ClassWithSeveralVariousAccessConstructors()
		public com.itcuties.java.reflection.constructors.data.ClassWithSeveralVariousAccessConstructors(int) throws java.lang.Exception
		com.itcuties.java.reflection.constructors.data.ClassWithSeveralVariousAccessConstructors(java.lang.Object)
		protected com.itcuties.java.reflection.constructors.data.ClassWithSeveralVariousAccessConstructors(boolean,byte,short,int,long)

And what would happen if we would call getConstructors() method using this code

    System.out.println("Enlisting visible constructors of the class.");
	for(Constructor<?> constructor : ClassWithSeveralVariousAccessConstructors.class.getConstructors()) {
		System.out.println("\t\t" + constructor);
	}

Only the public constructors would be displayed as shown here:

    Enlisting visible constructors of the class.
		public com.itcuties.java.reflection.constructors.data.ClassWithSeveralVariousAccessConstructors(int) throws java.lang.Exception

In the last example we will call getDeclaredConstructors() method on the class that has no constructor defined.

ClassWithDefaultConstructor

package com.itcuties.java.reflection.constructors.data;

public class ClassWithDefaultConstructor {

}
    System.out.println("Enlisting all constructors of the class with no constructor explicitly declarated.");
	for(Constructor<?> constructor : ClassWithDefaultConstructor.class.getDeclaredConstructors()) {
		System.out.println("\t\t" + constructor);
	}

This code will give us the following output.

    Enlisting all constructors of the class with no constructor explicitly declarated.
		public com.itcuties.java.reflection.constructors.data.ClassWithDefaultConstructor()

Don’t be surprised with this output. This is the default public constructor with no arguments that is generated by Java while no other constructor is being declared.

Download this sample code here.

This code is available on our GitHub repository as well.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Java by Example App is available at Google Play Store NOW