getting only name of the class Class.getName()

2020-02-20 05:56发布

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
Any Api can do that?

标签: java class
7条回答
迷人小祖宗
2楼-- · 2020-02-20 06:01

Here is the Groovy way of accessing object properties:

this.class.simpleName    # returns the simple name of the current class
查看更多
Ridiculous、
3楼-- · 2020-02-20 06:04

Get simple name instead of path.

String onlyClassName =  this.getLocalClassName(); 

call above method in onCreate

查看更多
Explosion°爆炸
4楼-- · 2020-02-20 06:18

or programmaticaly

String s = String.class.getName();
s = s.substring(s.lastIndexOf('.') + 1);
查看更多
可以哭但决不认输i
6楼-- · 2020-02-20 06:21

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(){
Log.e(TAG, "printVariable: ");
}

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:
查看更多
叼着烟拽天下
7楼-- · 2020-02-20 06:22

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: package.Student

The simple Class Name is: Student

查看更多
登录 后发表回答