Selva found some code to give you a way of getting hold of the current class name. For example, if you want to log a message and prefix it with the name of the class you are currently executing in (without using some logging framework like log4j..., and without hardcoding the class name in the message), I thought there would be a nice easy way to do it. It seems like not - you run into problems with static vs non-static contexts... But then Selva found this sample code somewhere:
public class ClassFromStatic {
public static void main(java.lang.String[] args) {
someStaticMethod();
}
public static void someStaticMethod() {
System.out.println("I'm in " + new CurrentClassGetter().getClassName()
+ " class");
}
public static class CurrentClassGetter extends SecurityManager {
public String getClassName() {
return getClassContext()[1].getName();
}
}
}
However I've heard that this approach uses deprecated methods. It compiles and runs OK in J2SDK 1.3 though.