Java Reflection Object Create








We can use reflection to create objects of a class dynamically. by invoking one of the constructors.

And then we can access the values of fields of objects, set their values, and invoke their methods.

There are two ways to create objects:

  • Using the no-args constructor
  • Using the constructor with arguments




no-args constructor

If you have the reference of a Class object, you can create an object of the class using the newInstance() method on the Class class.

This method takes no parameter and is equivalent to using the new operator on the no-args constructor of the class.

MyClass m  = myObject.newInstance();
class MyClass {/* w  w w  .  ja v a  2 s . co m*/
  public MyClass() {
     System.out.println("called");
  }
}
public class Main {
  public static void main(String[] args) throws InstantiationException {
    Class<MyClass> personClass = MyClass.class;
    try {
      MyClass p = personClass.newInstance();
      System.out.println(p);
    } catch (InstantiationException | IllegalAccessException e) {
      System.out.println(e.getMessage());
    }
  }
}

The code above generates the following result.





Constructor with argument

You can create an object using reflection by invoking a particular constructor. It involves two steps.

  • Get an instance of the constructor
  • Call newInstance to invoke it

You can get the reference of this constructor as shown:

Constructor<MyClass> cons  = myClass.getConstructor(int.class, String.class);

Then call the newInstance() method with the arguments to create an object.

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//from  w  w w  .j a  v a  2  s.c  om
class MyClass {
  public MyClass(int i, String s) {
    System.out.println("called");
    System.out.println(i);
    System.out.println(s);
  }
}
public class Main {
  public static void main(String[] args) {
    Class<MyClass> myClass = MyClass.class;
    try {
      Constructor<MyClass> cons = myClass.getConstructor(int.class,
          String.class);
      MyClass chris = cons.newInstance(1, "abc");
      System.out.println(chris);
    } catch (NoSuchMethodException | SecurityException | InstantiationException
        | IllegalAccessException | IllegalArgumentException
        | InvocationTargetException e) {
      System.out.println(e.getMessage());
    }
  }
}

The code above generates the following result.

Invoking Methods

We can invoke methods using reflection through the method reference.

To invoke a method, call the invoke() method on the method's reference.

Its first parameter is the object where it is from and the second parameter is a varargs for all the arguments in the same order as the method's declaration.

In case of a static method, we just need specify null for the first argument.

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//w  w  w . ja  v  a2 s.  c  o m
class MyClass {
  public MyClass() {
  }

  public void setName(String n) {
    System.out.println(n);
  }
}

public class Main {
  public static void main(String[] args) {
    Class<MyClass> myClass = MyClass.class;
    try {
      MyClass p = myClass.newInstance();
      Method setName = myClass.getMethod("setName", String.class);
      setName.invoke(p, "abc");
    } catch (InstantiationException | IllegalAccessException
        | NoSuchMethodException | SecurityException | IllegalArgumentException
        | InvocationTargetException e) {
      System.out.println(e.getMessage());
    }
  }
}

The code above generates the following result.