适用于非静态方法: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 当前类_Java获取当前类名的两种方法相关推荐

  1. java时间代码_java获取当前时间的四种方法代码实例

    这篇文章主要介绍了java获取当前时间的四种方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 第一种:Date类 public class ...

  2. java当前时间_java 获取当前时间的三种方法

    总结java里面关于获取当前时间的一些方法 System.currentTimeMillis() 获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响, ...

  3. Java获取当前类名的两种方法

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

  4. java 取随机整数_Java获取随机整数的两种方法

    方式一: 使用 Random 类 举例:获取 0~3 范围内(包括 0 和 3 )的 int 类型的随机数 Random random = new Random(); System.out.print ...

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

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

  6. JS基础-Java Class类以及获取Class实例的三种方式

    JS基础-Java Class类以及获取Class实例的三种方式 由于JVM为每个加载的class创建了对应的Class实例,并在实例中保存了该class的所有信息,包括类名.包名.父类.实现的接口. ...

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

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

  8. java web ip_详解Java Web如何限制访问的IP的两种方法

    前一阵子因为在做项目时碰到了这个功能,现在好好总结一下,至于为什么要限制IP访问,我就不多说了.然后百度了一下,现在主要有两种方式去限制IP访问,第一种是最简单的方便的,第二种是通过过滤器来限制访问. ...

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

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

最新文章

  1. java if (name!=null name!=),命名不规范,lombok泪两行!
  2. 使用Yum历史查找已安装或已删除的软件包信息
  3. xampp php5.6 7.1共存,New XAMPP with PHP 7.2.8, 7.1.20, 7.0.31 5.6.37
  4. mall-swarm是一套微服务商城系统
  5. MyBatis 实现增删CRUD
  6. YARN调度策略比较
  7. 微信小程序 - 豆瓣同城
  8. UISegmentedControl UISlider
  9. openssl内存分配,查看内存泄露
  10. Oracle1518,redhat 5.4 Oracle 10g r2
  11. PAIP.ecshop file_put_contents Warning notice解决方法
  12. 软件工程需求分析文档模板
  13. android实现自定义图标,Android开发中用Drawable 实现自定义电池图标
  14. 软件保护技术 - 基础
  15. Windows 命令提示符
  16. 【ACCV2022】论文阅读笔记Lightweight Alpha Matting Network Using Distillation-Based Channel Pruning
  17. company corporation enterprise 等等公司名称的区别
  18. javascript严格模式详解
  19. 一名合格的网络工程师,我们需要掌握哪些基本知识
  20. 六类网线钳能压五类水晶头吗_六类网线钳能压五类水晶头吗_网线水晶头线序的接法解析...

热门文章

  1. 字魂35号经典雅黑_2020:上海不锈钢黑钛线条行业
  2. MATLAB安装第三方工具箱
  3. Ubuntu中安装Eclipse的SVN插件——subclipse
  4. android拍照功能无预览,Android 无预览拍照
  5. mysql数据库日志截断,MySQL基础(十一):查询截取分析
  6. 前端全栈大佬是如何使用javaScript实现一个焦点图
  7. c语言调用话筒的程序,c – OpenAL:如何创建简单的“麦克风回声”程序?
  8. 网络工程属于计算机哪一类,网络工程专业属于什么门类
  9. jQuery之jQuery扩展和事件
  10. 【Set jsonObj = toJson( jsonString )】创建JSON实例