Java has methods, not functions. The difference is that methods have classes; you need to know the class to call the method. If this is an instance method, you need an instance to call it, but OTOH means you can easily view the method:
public void callByName(Object obj, String funcName) throws Exception {
obj.getClass().getDeclaredMethod(funcName).invoke(obj);
}
Note that there are many potential exceptions to this, and things get more complicated if you want to pass arguments.
If you are talking about a class method, then you are doing a little different:
public void callClassByName(Class cls, String funcName) throws Exception {
cls.getDeclaredMethod(funcName).invoke(null);
}
You can also explore with java.lang.reflect.Proxy.
source
share