import java.util.*; public class Test{ public static void main(String[] args){ Collections.sort(new ArrayList()); System.out.println("Main:" + amIFoo() ); foo(); } static void foo(){ System.out.println("Foo: " + amIFoo() ); } static boolean amIFoo(){ Throwable t = new Throwable(); StackTraceElement[] trace = t.getStackTrace(); String methodName =trace[1].getMethodName(); return methodName.equals("foo"); } }