Re: Java Reflection question
Patricia Shanahan wrote:
Albert Grein?cker wrote:
Hi NG,
maybe a too simple question: How can I get the type of a class within
a static method which belongs to the class for which I would like to
have the type?
thx,
Albert
I don't think there is any really satisfactory way. Here's a hack that
uses a nested class declaration:
public class NameTest {
public static void main(String[] args) {
Class myClass =
DummyForGettingClassName.class.getEnclosingClass();
System.out.println(myClass.getName());
}
private class DummyForGettingClassName{
}
}
I should perhaps point out that I never use this. If there were a really
smooth way of avoiding repeating the class name I might use it. As it
is, I would write:
Class myClass = NameTest.class;
If the class name is long enough for a risk of typos, use copy-paste.
Never change a class name by editing it directly. Instead, use a
refactoring tool such as Eclipse to rename it.
Patricia