This is my code:
class A {
public static void doIt() {
//Class currentClass = new Object() { }.getClass().getEnclosingClass();
//Class currentClass = MethodHandles.lookup().lookupClass();
String currentClass = Thread.currentThread().getStackTrace()[1].getClassName();
System.out.println("CALLING CLASS:" + currentClass);
}
}
class B extends A { }
public class NewMain {
public static void main(String[] args) {
A.doIt();
B.doIt();
}
}
As you see doIt
method can be called by A
and B
classes. In doIt
I want to know what class was used to call method (A
or B
). Is it possible? Three solutions I tried didn't work - it always says A
class.
Aucun commentaire:
Enregistrer un commentaire