Class.getMethod in class's static initializer block
- From: chucky <tomas.mikula@xxxxxxxxx>
- Date: Wed, 01 Aug 2007 18:19:38 -0000
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?
Actually, I would like to write something like this:
class A {
private static void method(String str) {
System.out.println(str);
}
static Method m = A.method;
}
Of course, this code is invalid, but my idea is that the presence of
method is known at compile time, so I don't want the overhead of
reflection and I'd rather get a compilation error if there is no such
method. Something similar is possible with function pointers in C, but
is there sth. like that in Java?
Thanks for any help!
.
- Follow-Ups:
- Re: Class.getMethod in class's static initializer block
- From: A. Bolmarcich
- Re: Class.getMethod in class's static initializer block
- From: Daniel Pitts
- Re: Class.getMethod in class's static initializer block
- Prev by Date: Re: Make GUI in Netbeans or Eclipse
- Next by Date: Re: Class.getMethod in class's static initializer block
- Previous by thread: Announce: VBeeJava, A VB.Net to Java solution: free trial and beta
- Next by thread: Re: Class.getMethod in class's static initializer block
- Index(es):
Relevant Pages
|