How to create object using reflection in Java

  • 27 October 2016
  • ADM

 

How to create object using reflection in Java - images/logos/java.jpg

 

Java Reflection provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions. Also provides the possibility to instantiate new objects, invoke methods and get/set field values.

Here is an example how to instantiate/create a new object using reflection in Java.

Example

Our test class will be a simple model class having 2 constructors.

package com.admfactory.reflect;

public class User {

    private int id;
    private String username;

    public User() {
	System.out.println("User object has been created using constructor User()");
    }

    public User(int id, String username) {
	this.id = id;
	this.username = username;
	System.out.println(String.format("User object has been created using constructor User(%d, %s)", id, username));
    }

    public int getId() {
	return id;
    }

    public void setId(int id) {
	this.id = id;
    }

    public String getUsername() {
	return username;
    }

    public void setUsername(String username) {
	this.username = username;
    }

    public void display() {
	System.out.println(String.format("ID: %d, username: %s", id, username));
    }

}

You can call both constructors with or without parameters.

To instantiate a new object without parameters:

Constructor<User> constr1 = clazz.getConstructor();
User user3 = constr1.newInstance();

To instantiate a new object with parameters:

Constructor<User> constr2 = clazz.getConstructor(int.class, String.class);
User user4 = constr2.newInstance(3, "user4");

For parameters, you need to provide the parameters types in getConstructor method and then add the parameters values when you call the newInstance method.

Here is the full printout for the example.

package com.admfactory.reflect;

import java.lang.reflect.Constructor;

public class ContructorTest {
    public static void main(String[] args) throws Exception {

	System.out.println("Contructor instantiation using reflection example");
	System.out.println();
	System.out.println("Normal instantiation");
	System.out.println();
	User user1 = new User();
	System.out.println();
	User user2 = new User(2, "user2");

	System.out.println();
	System.out.println("Instantiation using reflection");
	System.out.println();
	
	Class<User> clazz = User.class;
	
	Constructor<User> constr1 = clazz.getConstructor();
	User user3 = constr1.newInstance();
	
	Constructor<User> constr2 = clazz.getConstructor(int.class, String.class);
	User user4 = constr2.newInstance(3, "user4");
    }
}

Output

Contructor instantiation using reflection example

Normal instantiation

User object has been created using constructor User()

User object has been created using constructor User(2, user2)

Instantiation using reflection

User object has been created using constructor User()
User object has been created using constructor User(3, user4)

You can notice that both methods of getting a new object, using the new keyword or using reflection generate the same output.

 

References