Wednesday, August 24, 2011

Calling Java methods dynamically by passing a String of the method name

Imagine you have 99 distinct methods:
  • method1()
  • method2()
  • method3()
  • method4()
  • ...
  • ...
  • ...
  • method99()
and each of these methods are distinct methods with distinct functionality which will be executed for the 99 distinct return values of your getID() method.

So typically you would have to write 99 if-else condition statements. What if you could simply do the same thing in one line instead of defining 99 if-else condition statements.
call_method ("method" + getID());
The following code snippet is a simple tutorial of how to use STRING parameters for calling methods dynamically in java.