| Refresh | Home EGTry.com

invoke a method that take arguments and return a value


package reflection;

import java.lang.reflect.Method;

public class InstanceMethodWithArg {

	public static void main(String[] args) throws Exception {
		Class clazz=Class.forName("reflection.InstanceMethodWithArg");
		Object obj=clazz.newInstance();
		Method method1=clazz.getMethod("add", Integer.TYPE, Integer.TYPE);
		int sum=(Integer)method1.invoke(obj, 2,4);
		System.out.println("the sum is: "+sum);
	}
	

	public int add(int a, int b) {
		return a+b;
	}
	


}