In C and C + + you can print the current function name in this way:
Copy Code code as follows:
In Java, however, there are two ways in which objects can be obtained from an object:
The first: through the thread class to get.
Copy Code code as follows:
System.out.println (Thread.CurrentThread (). Getstacktrace () [1].getmethodname ());
System.out.println (Thread.CurrentThread (). Getstacktrace () [1].getclassname ());
The second type is obtained by the Throwable class, and the second method can be extended, as long as the Throwable subclass can implement the function.
Copy Code code as follows:
System.out.println (New Throwable (). Getstacktrace () [0].getmethodname ());
System.out.println (New Throwable (). Getstacktrace () [0].getclassname ());
There are two ways to print out the current method name and the class name in it, but one drawback is the following index:
Copy Code code as follows:
What's the value, 0 or 1, or some other number, where the index of the two ways is written 0 and 1, which is used to remind yourself that it is not fixed and needs to be determined by testing itself. It is said that JDK versions may not be the same value. But I didn't test it myself.
JDK version of my test: Java version "1.7.0_17"