Java獲取當(dāng)前類名的兩種方法

獲取類名:


1、在類的實(shí)例中可使用this.getClass().getName();但在static method中不能使用該方法;


2、在static method中使用方法:Thread.currentThread().getStackTrace()[1].getClassName();


獲取方法名:Thread.currentThread().getStackTrace()[1].getMethodName();


獲取代碼行號:Thread.currentThread().getStackTrace()[1].getLineNumber();



Log 代碼:


System.out.println("Class: "+this.getClass().getName()+" method: "+

Thread.currentThread().getStackTrace()[1].getMethodName() +" line:"+

Thread.currentThread().getStackTrace()[1].getLineNumber());

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,734評論 18 399
  • 進(jìn)程和線程 進(jìn)程 所有運(yùn)行中的任務(wù)通常對應(yīng)一個進(jìn)程,當(dāng)一個程序進(jìn)入內(nèi)存運(yùn)行時,即變成一個進(jìn)程.進(jìn)程是處于運(yùn)行過程中...
    勝浩_ae28閱讀 5,257評論 0 23
  • 文件名 Thread.currentThread().getStackTrace()[originStackInd...
    __Andy__閱讀 1,294評論 0 1
  • This morning, my colleague share an article “why we desir...
    sue諾閱讀 173評論 0 0
  • 聰明才智心中藏, 犀利眼光惹鋒芒。 不求日后風(fēng)光顯, 但求將來舞臺王。
    遨游于天際閱讀 211評論 0 1

友情鏈接更多精彩內(nèi)容