I want to call a method using reflection.
The method is:
public String format(String text,int value)
{
return text+" "+value;
}
So, it has 2 arguments: a String and an int. And it returns a String. How can I call it?
I want to call a method using reflection.
The method is:
public String format(String text,int value)
{
return text+" "+value;
}
So, it has 2 arguments: a String and an int. And it returns a String. How can I call it?
try {
Class<?> type = Foo.class;
Method method = type.getMethod("format", String.class, int.class);
//as the method is not static, you need to have an instance of the class to be able to invoke the method
Foo instance = new Foo();
String string = (String) method.invoke(instance, "string", 42);
} catch (Exception toBeHandled) {}
and replace the Foo with the name of your class