适用于非静态方法:this.getClass().getName()

适用于静态方法:Thread.currentThread().getStackTrace()[1].getClassName()

获取类名:

1、在类的实例中可使用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());

http://blog.sina.com.cn/s/blog_4a4f9fb50101eyfp.html

Description

Below I present you two different ways to get the current Class:

  • Using Thread
  • Using getClass()

getClass() method present in every Java object. Like here:

String clazz = this.getClass().getName();

static method. It won't work. Even the keyword this is meaningless in a static method.

Also, the class returned by the above method may actually be a subclass of the class in which the method is defined. This is because subclasses inherit the methods of their parents; and getClass() returns the actual runtime type of the object. To get the actual class in which a method is defined, use the method below also.

static method you can instead use the following:

String clazz = Thread.currentThread().getStackTrace()[1].getClassName();

Which uses the static methodgetStackTrace() to get the whole stacktrace. This method returns an array, where the first element (index 0) is the getStackTrace() you called and the second element (index 1) is the method your code is in.

String method = Thread.currentThread().getStackTrace()[1].getMethodName();

The code

package org.wikijava.reflection;  
  public class MethodName {   public static void main(String[] args) { 
 MethodName methodName = new MethodName();   
  String clazz = Thread.currentThread() .getStackTrace()[1].getClassName();
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
System.out.println("class name: " + clazz + " Method Name " + method); 
  methodName.anotherMethod(); }   
  private void anotherMethod() { 
  String clazz = this.getClass().getName(); 
  String method = Thread.currentThread() .getStackTrace()[1].getMethodName(); 
  System.out.println("class name: " + clazz + " Method Name " + method);   }   }

Java获取当前类名的两种方法相关推荐

  1. java 当前类_Java获取当前类名的两种方法

    适用于非静态方法:this.getClass().getName() 适用于静态方法:Thread.currentThread().getStackTrace()[1].getClassName() ...

  2. Java 跨平台获取 MAC 地址的两种方法

    前言 有时我们因为软件授权或者其它需要获取主机唯一标识而需要获取用户主机的MAC地址,而本文则将介绍如何通过Java来实现跨平台获取MAC地址的两种方法,同时具体的代码也已上传到GitHub. 如果不 ...

  3. JAVA获取安卓系统下usb_Android 获取 usb 权限的两种方法

    前言: 最近工作上遇到几个USB模块在android平台上适配使用的情况,所以要用到USB权限获取问题 ##USB权限获取有以下2种方式: 一.直接在AndroidManifest.xml文件中进行如 ...

  4. java 获取当前时间的三种方法是什么

    本文主要介绍java获取当前时间的三种方法.这篇文章很详细,有一定的参考价值.有需要的朋友可以参考一下. 总结一些java中获取当前时间的方法. System.currentTimeMillis() ...

  5. 获取手机联系人的两种方法

    获取手机联系人的两种方法 第一种方法比较简单 就几句话 但是获取的信息不完善 第二种方法比较难 代码比较多 获取的信息全 须要加的权限 : <uses-permission android:na ...

  6. java 匿名list,java创造匿名对象的两种方法

    在java中有时候需要一些匿名对象的使用.可能有些小伙伴拿还不会创造,其实我们在学习一些方法时都或多或少的接触过.本篇所要讲到的创造匿名对象总结了两种方法,分别是静态工具方法和Lambda表达式,我们 ...

  7. root android oppo,OPPO R9S怎么ROOT oppor9s获取root权限的两种方法

    OPPO R9S如何ROOT呢?作为一款最新的OPPO系列智能手机,对于刚入手的用户而言,可能不知该如何获取OPPO R9S手机ROOT权限.下面就与大家分享一下获取OPPO R9S手机ROOT权限的 ...

  8. 关于java获取本地ip的几种方法

    关于java获取本地ip的几种方法 1.获取本地ip的方法 System.out.println(InetAddress.getLocalHost().getHostAddress()); 2.域名解 ...

  9. ENVI获取地物波谱特征的两种方法

    ENVI获取地物波谱特征的两种方法 以Landsat8遥感影像为例 [方法一] 首先利用[Layer Stacking]将不同波段的影响合成一幅多光谱影像(一定要按照波长顺序,如果是Landsat8影 ...

最新文章

  1. pl/sql连接数据库
  2. Codeforces Round #497 (Div. 1)
  3. vs2019编写Linux c/c++项目
  4. 海南首例供港造血干细胞志愿者启程赴广东捐献
  5. wrapper怎么用_用责任链模式设计拦截器
  6. java for android的书_JavaForAndroid07
  7. Swagger原理解析
  8. Win7的常用快捷键以及设置
  9. Android MVP 实例
  10. 早上运动和晚上运动,哪个更利于减肥?
  11. Maven下载安装旧版本
  12. windows下,本地硬盘搜索工具everything。适合windows下搜索文件名。
  13. DHT11温湿度传感器学习
  14. 抖音快手短视频批量去水印采集软件v1.8使用文档
  15. (原)Opencv中直方图均衡和图像动态范围拉伸的代码
  16. html骂人网页,网页前台通过js非法字符过滤代码(骂人的话等等)
  17. 好用的mac软件下载网站
  18. RK平台之AI模型转换环境搭建
  19. java的发展简史、简介以及学习Java的意义
  20. Mysql的ER图怎么画_分享——如何画数据库ER图   王慧芬(10100340214)

热门文章

  1. 我的MYSQL学习心得(六) 函数
  2. 威胁预警|多个挖矿僵尸网络开始使用ThinkPHP v5漏洞 威胁升级
  3. devops 成长路线
  4. 购物商城---购物车,结算
  5. (原)vs2013编译boost1.60库
  6. Linux_IPtables防火墙详解
  7. ecshop去掉“云服务中心”或者是“模板堂知识库”
  8. 001.android初级篇之ToolBar
  9. 猴子请来的逗比项目流水总结
  10. SqlServer int型转varchar型 出现*号