What is reflection API?

reflection in Java

In Java, reflection is an API that is used to check and modify the behavior of the interfaces, classes, and methods at the runtime.

Reflection API

The classes which are required for the reflection are availabe in java.lang.reflect package which is very necessary to understand the reflection.

Below is the illustration for better understanding.

java.lang.reflect package
  • Reflection is used to get the information about the class to which class the object belongs and also the information about which methods of the class can be executed by using the object.

  • With the help of reflection we can invoke the methods at the run time.

Reflection is used to get information about the classes, methods, and constructors.

  • class: The get the name of the class to which the object belongs, we use the getClass() method.

  • method:To get the methods of the class to which the object belongs, we use the getMethods() method.

  • constructor: To get the public constructors of the class to which the object belongs, we use the getConstructors() method.

We can invoke a method if we know its name and the parameter types. For this purpose we can use two methods:

  • The getDeclaredMethod()

  • The invoke() method

The getDeclaredMethod()

It is used to create the object of the method which we want to invoke.

Syntax

Class.getDeclaredMethod(name, parameter)

Parameters

  • name: It is the name of the method by which we want to create the object.

  • parameter: It is an array of the objects of the class.

The invoke()method

It is used to invoke the methods of the class at the runtime.

Syntax

Method.invoke(Object, parameter)

Code example

Let’s discuss the basic example to demonstrate the use of these methods:

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
class Test {
private String str;
public Test() { str = "Behzad Ahmad"; }
public void m1()
{
System.out.println("String: " + str);
}
public void m2(int num)
{
System.out.println("Number: " + num);
}
private void m3()
{
System.out.println("In Private Method");
}
}
class main
{
public static void main(String args[]) throws Exception
{
Test obj = new Test();
Class<?> cls = obj.getClass();
System.out.println("Class Name is: " + cls.getName());
Constructor constructor = cls.getConstructor();
System.out.println("Constructor Name is: " + constructor.getName());
Method[] methods = cls.getMethods();
for (Method method : methods)
System.out.println(method.getName());
Method methodcall1 = cls.getDeclaredMethod("m2", int.class);
methodcall1.invoke(obj, 21);
}
}

Code explanation

  • Lines 1-3: We import the required classes.

  • Line 5: We make a class where we want to create the object.

  • Line 6: We create a private field.

  • Lines 8-23: We make four constructors of the class, the first one is the public constructor. The second constructor takes no arguments, the third accepts a string as an argument, and the last constructor is private.

  • Line 25: We make the main class.

  • Line 27: We invoke the main driver method.

  • Line 29: We create the object of class 1 whose property we want to check.

  • Lines 31-32: We create the class object by using the getClass() method and print its name using the getName() method.

  • Lines 34-35: We get the constructor by using the object of the class and print the name of the constructor.

  • Lines 37-39: We get the method names by using the object of the class through the getmethods() method and print the names of the methods.

  • Lines 41-43: We create the object of the desired method by providing the name of the method and the parameter class as the arguments to the getDeclaredMethod() and invoke it at run time.

Free Resources