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