Is it possible to call a method by reflection from a class?
class MyObject {
... //some methods
public void fce() {
//call another method of this object via reflection?
}
}
Thank you.
Absolutely:
import java.lang.reflect.*;
public class Test
{
public static void main(String args[]) throws Exception
{
Test test = new Test();
Method method = Test.class.getMethod("sayHello");
method.invoke(test);
}
public void sayHello()
{
System.out.println("Hello!");
}
}
If you have problems, post a specific question (preferrably with a short but complete program demonstrating the problem) and we'll try to sort it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With