title: How does the JAVA reflection API know which method to pass the parameters to?
body: <p>I have 4 different methods, where only one requires a parameter:</p>
  
  <pre><code>foo1 (){}
  foo2 (){}
  foo3 (String data) {}
  foo4 (){}
  </code></pre>
  
  <p>I am invoking these methods using the Java Reflection API. My code looks like this at the moment:</p>
  
  <pre><code>            Mymethod method = new ActionKeyWords();
  
              Method [] methods = method.getClass().getDeclaredMethods();
  
              for (Method s : methods) {
  
                      if (s.getName().contains("foo3")) {
  
                          System.out.println("Executing method: " + s.getName());
                          s.invoke(s.getName(),"data");
  
  
                      else
                          System.out.println("Executing method: " + s.getName());
                          s.invoke(s.getName());
  
                  }
  
              }
  </code></pre>
  
  <p>You can see that I am making a control to see if the methods name is foo3 before invoking the method. That is because that method is the only method I want to pass a  parameter too. If I remove the if statement I get a <code>"java.lang.IllegalArgumentException: wrong number of arguments".</code></p>
  
  <p>How else would the API know which function I want to pass a parameter to? At the moment only foo3 requires a parameter, but what if foo98 requires it as well. </p>
  
  <p>The reason why I chose the reflection API was to get rid of the endless if statements in my code.</p>
  
  <p>Is this the only solution? Is there a smarter way to solve this? </p>