Invoke constructor using Reflection in java

In this post, we will see how to invoke constructor using reflection in java.

You can retrieve the constructors of the classes and instantiate object at run time using reflection. java.lang.reflect.Constructor is used to instantiate the objects.

Instantiate Constructor with no parameters

In case, you want to create object using no args constructor at runtime, you can do it as below.

Here,
Class.forName("org.arpit.java2blog.Color") is used to load the class and get an object of type Class and we can retrieve a lot of information such as constructors, methods, and annotations etc. at run time with Class object.

You need to replace org.arpit.java2blog.Color with classname for which you want to instantiate object at run time.

cl.newInstance() can be used to create object with no-args constructor at run time.

Instantiate Constructor with parameters

In case, you want to instantiate constructor with parameters, you need to retrieve constructor with parameters and then pass parameters to instantiate it.

  • You need to pass Class[] to getConstructor() method and retrieve the instance of java.lang.reflect.Constructor from cl.
  • Pass Object[] to cons.newInstance to construct object with passed parameters.

You can also get Parameters types with cons.getParameterTypes()

Let’s see this with an example.
Consider a class named Color which has two attributes name and htmlCode.

Create main class named ConstuctorReflectionMain

When you run above program, you will get below output:
Output:

====================
Calling default constructor
====================
Color [name:black HtmlCode:#000000]
====================
Calling parameterized constructor
====================
Color [name:Red HtmlCode:#FF0000]

As you can see, we are able to instantiate object using java.lang.reflect.Constructor at run time using reflection.

Constructor parameters for primitive types

In case, you have constructor parameters with primitive types, you can use .class suffix with that.
For example:
If the parameter type is of int, then you can use int.class while getting constructor of that type.

Let’s understand with the help of example:
Consider a class Employee with attributes: name and age.

Create main class named ConstructorParameterPrimMain

When you run above program, you will get below output:
Output:

Employee [name=Arpit, age=28]

You can also pass Integer.Type instead of int.class.

Conclusion

You have learnt about how to invoke constructor and instantiate objects at runtime using reflection. We have also seen how to handle primitive type parameters while getting constructor from class.
That’s all about how to invoke constructor using Reflection in java.

Related Posts

  • 15 May

    Get and set Fields using reflection in java

    In this post, we will see how to get and set fields using reflection in java. java.lang.reflect.Field can be used to get/set fields(member variables) at runtime using reflection. Get all fields of the class All fields of the class can be obtained from the Class object. Here is an example. [crayon-60090e0d37d87351477956/] Field[] will have all […]

  • 15 May

    Access private fields and methods using reflection in java

    In this post, we will see how to access private fields and methods using reflection in java. Can you access private fields and methods using reflection? Yes, you can. It is very easy as well. You just need to call .setAccessible(true) on field or method object which you want to access. Access private field Class.getDeclaredField(String […]

  • 24 October

    Invoke Getters And Setters Using Reflection in java

    In this post, we will see how to call getters and setters using reflection in java. We have already seen how to invoke method using reflection in java. There are two ways to invoke getter and setter using reflection in java. Using PropertyDescriptor You can use PropertyDescriptor to call getters and setters using reflection. Getter: […]

  • 23 September

    How to invoke method using reflection in java

    In this post, we will see how to invoke the method using reflection in java. Let’s understand this with the help of the example. Create a class named Employee.java. We will invoke this class’s method using reflection. [crayon-60090e0d38138507566654/] Create a main method named EmployeeReflectionMain.java. [crayon-60090e0d3813c170727434/] When you run above program, you will get below output: […]

  • 23 September

    Java Reflection tutorial

    Introduction Reflection, as we know from the common term, is an image of yourself or something else you see in a mirror or a mirrored surface. Java, being an object-oriented language draws heavily from real-life examples and so is the Reflection API of Java inspired from real life. In Java, the process of analyzing, viewing […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.