包含由当前堆栈跟踪元素表示的执行点的当前方法名称由java.lang.StackTraceElement.getMethodName()方法提供。
演示此的程序如下所示-
public class Demo {
public static void main(String args[]) {
System.out.println
("The method name is: " + new Exception().getStackTrace()[0].getMethodName());
}
}输出结果
The method name is: main
现在让我们了解上面的程序。
该方法getMethodName()用于获取当前方法名称,该名称包含由当前堆栈跟踪元素表示的执行点。使用System.out.println()打印。演示这的代码片段如下-
System.out.println
("The method name is: " + new Exception().getStackTrace()[0].getMethodName());