Re: Class.getMethod in class's static initializer block



On 2007-08-01, chucky <tomas.mikula@xxxxxxxxx> wrote:
If I call A.class.getMethod() from static initializer block of class
A, I get NoSuchMethodException.

Example:

class A {
static Method m;

private static void method(String str) {
System.out.println(str);
}

static {
try {
m = A.class.getMethod("method", new Class[] {String.class});
} catch(NoSuchMethodException e) {
throw new ExceptionInInitializerError(e);
}
}
}

This code always throws the ExceptionInInitializerError caused by
NoSuchMethodException.
Why does this happen?
[snip]

It happens because according to the Java API documentation, getMethod:
"Returns a Method object that reflects the specified public member
method of the class or interface represented by this Class object."
Note the term "public member method".

Did you intend to invoke the getDelcaredMethod?
.