Re: Class.getMethod in class's static initializer block
- From: "A. Bolmarcich" <aggedor@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 01 Aug 2007 18:34:27 -0000
On 2007-08-01, chucky <tomas.mikula@xxxxxxxxx> wrote:
If I call A.class.getMethod() from static initializer block of class[snip]
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?
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?
.
- Follow-Ups:
- References:
- Class.getMethod in class's static initializer block
- From: chucky
- Class.getMethod in class's static initializer block
- Prev by Date: Re: Dynamic variables
- Next by Date: Re: Updating JTree When Adding Nodes
- Previous by thread: Re: Class.getMethod in class's static initializer block
- Next by thread: Re: Class.getMethod in class's static initializer block
- Index(es):