package com.extend.test; public class GetMethodName { public static void main(String[] args) { String classFile = Thread.currentThread().getStackTrace()[1] .getFileName(); String methodName = Thread.currentThread().getStackTrace()[1] .getMethodName(); int methodNameLine = Thread.currentThread().getStackTrace()[1] .getLineNumber(); String className = Thread.currentThread().getStackTrace()[1] .getClassName(); System.out.println("类文件名是:" + classFile); System.out.println("当前方法是:" + methodName); System.out.println("当前行号:" + methodNameLine); System.out.println("类名是:" + className); } } 运行结果: 类文件名是:GetMethodName.java 当前方法是:main 当前行号:9 类名是:com.extend.test.GetMethodName