Java获取当前类名和当前方法名

来源:互联网 发布:htconemini知乎 编辑:程序博客网 时间:2024/06/05 23:53

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

适用于非静态方法: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();

原创粉丝点击