What you are looking for is reflection, here is a similar question: http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string
krock
2010-06-14 15:28:14
What you are looking for is reflection, here is a similar question: http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string
Easily done with reflection. Some examples here and here.
The main bits of code being
String aMethod = "myMethod";
Object iClass = thisClass.newInstance();
// get the method
Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
// call the method
thisMethod.invoke(iClass, paramsObj);
Yes, you can, using reflection. However, consider also Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection. If at all possible, use interfaces instead. Reflection is rarely truly needed in general application code.
With the reflection API. Something like this:
Method method = getClass().getDeclaredMethod(functionName);
method.invoke(this);