Created
October 1, 2019 13:02
-
-
Save giuliobosco/bfa8aaacfdcf5a815929c2c9a875e0ac to your computer and use it in GitHub Desktop.
Call B.m() from C.main() and it should print "content"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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