Program to demonstrate method invocation in Java using Method class in Reflection API
Output of the program :
package com.hubberspot.example;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Operations {
public int add(int num1, int num2) {
return num1 + num2;
}
public int subtract(int num1, int num2) {
return num1 - num2;
}
public int multiply(int num1, int num2) {
return num1 * num2;
}
public double division(int num1, int num2) {
return num1 / num2;
}
}
public class MethodInvocation {
public static void main(String[] args) {
// Creating Object of Operations
Operations operation = new Operations();
// Getting the Class associated with Operations
Class classOperations= operation.getClass();
try {
// Calling getMethod method on classOperations
// by passing a string which specifies method
// name and two instance of Class which denotes
// an int parameter type
Method add = classOperations.getMethod("add",new Class[] {int.class,int.class});
// calling the invoke method over the Method
// by passing it the object of class to
// which the method is going to be invoked
// alongwith an array of Object which specified
// the parameters
Object result = add.invoke(operation,new Object[] { 20 , 20});
System.out.print("Calling "+add.getName()+
" method and result is : ");
System.out.println(result.toString());
// Calling getMethod method on classOperations
// by passing a string which specifies method
// name and two instance of Class which denotes
// an int parameter type
Method subtract = classOperations.getMethod("subtract",new Class[] {int.class,int.class});
// calling the invoke method over the Method
// by passing it the object of class to
// which the method is going to be invoked
// alongwith an array of Object which specified
// the parameters
result = subtract.invoke(operation,
new Object[] { 30 , 20});
System.out.print("Calling "+subtract.getName()+ " method and result is : ");
System.out.println(result.toString());
// Calling getMethod method on classOperations
// by passing a string which specifies method
// name and two instance of Class which denotes
// an int parameter type
Method multiply = classOperations.getMethod("multiply",new Class[] {int.class,int.class});
// calling the invoke method over the Method
// by passing it the object of class to
// which the method is going to be invoked
// alongwith an array of Object which specified
// the parameters
result = multiply.invoke(operation, new Object[] { 10 , 10});
System.out.print("Calling "+multiply.getName()+ " method and result is : ");
System.out.println(result.toString());
// Calling getMethod method on classOperations
// by passing a string which specifies method
// name and two instance of Class which denotes
// an int parameter type
Method division = classOperations.getMethod("division", new Class[] {int.class,int.class});
// calling the invoke method over the Method
// by passing it the object of class to
// which the method is going to be invoked
// alongwith an array of Object which specified
// the parameters
result = division.invoke(operation, new Object[] { 20 , 10});
System.out.print("Calling "+division.getName()+ " method and result is : ");
System.out.println(result.toString());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
Output of the program :
