Skip to content

Instantly share code, notes, and snippets.

@giuliobosco
Created October 1, 2019 13:02
Show Gist options
  • Save giuliobosco/bfa8aaacfdcf5a815929c2c9a875e0ac to your computer and use it in GitHub Desktop.
Save giuliobosco/bfa8aaacfdcf5a815929c2c9a875e0ac to your computer and use it in GitHub Desktop.
Call B.m() from C.main() and it should print "content"
// The idea is of call B.m() from C.main() and it should print "content"
// Calling B.m() from B.main() works correctly but Calling it from C.main() doesn't works...
public abstract class A {
public static void m() {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
StackTraceElement element = elements[elements.length - 1];
try {
Class classe = Class.forName(element.getClassName());
System.out.println(classe.getField("ATTR").get(classe));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class B extends A {
public static final String ATTR = "content";
public static void main(String[] args) {
B.m();
}
}
public class C {
public static void main(String[] args) {
B.m();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment