适用于非静态方法: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 Javaobject. 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 集合 List 分组的两种方法

    CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...

  2. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  3. tensorflow中的函数获取Tensor维度的两种方法:

    获取Tensor维度的两种方法: Tensor.get_shape() 返回TensorShape对象, 如果需要确定的数值而把TensorShape当作list使用,肯定是不行的. 需要调用Tens ...

  4. APP自动化测试获取包名的两种方法

    获取包名的两种方法: 一.通过aapt获取 1.进入aapt.exe所在路径 2.在地址栏输入cmd回车,打开dos命令窗口. 3.在命令窗口输入 aapt dump badging 拖入apk 回车 ...

  5. java字符串大小写转换的两种方法

    转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString {          pu ...

  6. Java获取ip地址的几种方法

    以下内容介绍下java获取ip地址的几种思路. 1.直接利用java.net.InetAddress类获取,不过这种方法只在windows环境下有效,在linux环境下只能获取localhost地址( ...

  7. 微信网页开发之获取用户unionID的两种方法--基于微信的多点登录用户识别

    假设网站A有以下功能需求:1,pc端微信扫码登录:2,微信浏览器中的静默登录功能需求,这两种需求就需要用到用户的unionID,这样才能在多个登录点(终端)识别用户.那么这两种需求下用户的unionI ...

  8. Java获取文件Content-Type的四种方法

    HTTP Content-Type在线工具 有时候我们需要获取本地文件的Content-Type,已知 Jdk 自带了三种方式来获取文件类型. 另外还有第三方包 Magic 也提供了API.Magic ...

  9. docker inspect获取详细参数的两种方法

    docker inspect xx 返回的是一个json格式的数据 以下为部分返回值 [ { "Id": "706813b0da107c4d43c61e3db9da908 ...

随机推荐

  1. wpf 千位符 格式化字符串

    StringFormat={}{0:N2}}//格式话字符串,增加千位符,2保留小数点后2位 StringFormat={}{0:N0}}//格式话字符串,增加千位符,无小数点后

  2. java代码分页

    分页类 这个适用情况: 适用于前端页面已提供分页按钮样式的情况 分页规则: 首页,尾页,上页,下页 这四个按钮必定出现,中间分页动态生成5个 如:首 上 2 3 4 5 6 下 尾 public cl ...

  3. Mybatis输入映射和输出映射

    本节内容: 输入参数映射 输出映射 resultMap Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 一.环境准备 复制 ...

  4. 第一届CCF软件能力认证

    1.相反数 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数). 输入格式 第一行包含一个正整数 N.(1 ≤ N ≤ 500). 第二行为 ...

  5. 图学ES6-3.变量的解构赋值

  6. JS中的的"闭包"?深入Javascript之this

    看了知乎上的话题 如何才能通俗易懂的解释javascript里面的‘闭包’?,受到一些启发,因此结合实例将回答中几个精要的答案做一个简单的分析以便加深理解. 1. "闭包就是跨作用域访问变量 ...

  7. Linux性能优化之内存优化(二)

    前言 不知道大家看完前面一章关于CPU优化,是否受到相应的启发呢?如果遇到任何问题,可以留言和一起探讨这方面的问题.接下来我们介绍一些关于内存方面的知识.内存管理软件包括虚拟内存系统.地址转换.交换. ...

  8. Ansible专题整理

    Ansible 专题文章总览 Ansible小手册,仅供参考 文章如未明确说明实验环境,默认如下: OS:Centos 6.7 x86_64 Ansible: 2.1.2.0 Python: 2.6. ...

  9. 使用Synchronized块同步方法

    synchronized关键字有两种用法.第一种就是在<使用Synchronized关键字同步类方法>一文中所介绍的直接用在方法的定义中.另外一种就是synchronized块.我们不仅可 ...

  10. ARP欺骗防御工具arpon

    ARP欺骗防御工具arpon   ARP欺骗是局域网最为常见的中人间攻击实施方式.Kali Linux提供一款专用防御工具arpon.该工具提供三种防御方式,如静态ARP防御SARPI.动态ARP防御 ...