0

I am using the getMethod(String name) function to load a method but it always throws MethodNotFoundException. If I run class.getMethods() the method I am looking for is in that result with the exact name I am using to pass to getMethod(). The method I am trying to load is a static method so I don't know if getMethod() will not work for static methods. Any help?

2
  • 2
    Let's see the code that's failing and the output you feel validates your code. :) Commented Sep 30, 2011 at 18:36
  • It works for static methodes, too. That's not the problem. -> en.wikibooks.org/wiki/Java_Programming/Reflection/Overview Commented Sep 30, 2011 at 18:36

2 Answers 2

6

If the method you're looking for takes any arguments, you need to pass their types to getMethod() as well. A Java method's signature (the thing that uniquely defines and identifies a method) is comprised of the method name and its parameter types.

http://download.oracle.com/javase/tutorial/java/javaOO/methods.html

Sign up to request clarification or add additional context in comments.

1 Comment

+1, and if it's non-public you need to use getDeclaredMethod instead.
0

The name isn't enough. You have to specify exactly which argument types you think the method takes, otherwise the query might be ambiguous (because Java supports overloading).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.