茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672169 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00017.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2019/4/2 5:44:27
getting only name of the class Class.getName()

How can i get the name of the class
String.class.getName()  returns java.lang.String

I am only interested in getting last part ie only String



Class.getSimpleName()




The below both ways works fine.
System.out.println("The Class Name is: " + this.getClass().getName());
System.out.println("The simple Class Name is: " + this.getClass().getSimpleName());

Output as below:
    The Class Name is: DemoJava.Student
    The simple Class Name is: Student




or programmaticaly
String s = String.class.getName();
s = s.substring(s.lastIndexOf('.') + 1);




You can use following simple technique for print log with class name.
private String TAG = MainActivity.class.getSimpleName();

Suppose we have to check coming variable value in method then we can use log like bellow :
private void printVariable(int variable){
Log.e(TAG, "printVariable: "+variable);
}

Importance of this line is that, we can check method name along with class name. To write this type of log.
write :- loge and Enter.
will print on console
E/MainActivity: printVariable: 5




Social.class.getSimpleName()

getSimpleName() : Returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous. The simple name of an array is the simple name of the component type with "[]" appended. In particular the simple name of an array whose component type is anonymous is "[]".



To get simple class name without any reference or path use..
String onlyClassName =  getLocalClassName();


原文出處:java - getting only name of the class Class.getName() - Stack Overflow
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|