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.

8 comments:

  1. imagine you have
    method1(int, char) , 
    method2(int, char) , 
    method3(int, char) , 
    method4(int, char) , 
    method5(int, char) , 
    method6(int, char) , 
    method7(int, char) , 
    method8(int, char) , 
    method9(int, char) , 
    method10(int, char).

    i had about 56 of them in my project....... and i replaced 500 lines of IF-ELSE conditions with just 10 lines.

    all i did was create a method called
    runMethod(String methodName , int myInt , char myChar);
    and execute it by calling
    runMethod("method"+number , myInt , myChar);

    helped me out.

    ReplyDelete
  2. maybe because i am not a very good programmer....
    or maybe because i did not know this dynamic programming before

    ReplyDelete
  3. hmm. how did you manage to create 56 methods !?! that seems overkill !

    ReplyDelete
  4. type of parameters field is varargs right ?

    ReplyDelete
  5. yes.

    well, it's specific to the public method that you would want to invoke.

    in this example, i am trying to call the method which has the following signature 
    public String myPublicMethod (int a, boolean b, char c, String d).

    So the name of the public method is "myPublicMethod" and the input parameters that this method is able to take are (int , boolean , char , String) in that sequence.

    Thus, when you make an instance of the method that you would want to invoke later, you need to let it know the name of the public method and the sequence of parameters that the method is capable of taking.

    so you literally type
    method = getClass.getMethod("myPublicMethod" , int.class , boolean.class, char.class , String.class );

    the *.class* is important..... because just int or just boolean or just char is a declaration of a parameter TYPE.... not a parameter itself..... for this context, you have to give in a PARAMETER...... thus the .class is necessary.

    let me know if i could answer your question.

    ReplyDelete
  6. hmm. i get it. i can differentiate the methods by changing the method name string dynamically. but all the methods that I'm going to use in that way must also have similar parameters. or else im gonna have to type all possible parameter type classes. am I right ?

    ReplyDelete
  7. it can vary...

    suppose you have 2 methods by the same name but different arguments:
    myPublicMethod(int , boolean , char);
    myPublicMethod(int, char);

    so here, you will have to have separate method instances for each of the public methods like this:

    Method myMethod1 = getClass.getMethod(
    "myPublicMethod" , int.class , boolean.class, char.class
    );
    Method myMethod2 = getClass.getMethod(
    "myPublicMethod" , int.class , char.class
    );

    then you can invoke the desired method instance that you want.

    ReplyDelete
  8. that kinda defeats the purpose doesn't it :P thanks.

    ReplyDelete