How to get the class name / method name running in Java

In Java, it is a method to get the name of the class or method being executed. Since it uses only the standard library, you can copy and paste it immediately.

Get class name

Get from run-time class

Example.java


public class Example {
	public static void main(final String[] args) {
		final String className = new Object(){}.getClass().getEnclosingClass().getName();
		System.out.println(className);
	}
}

output


Example

Get from stack trace

Example.java


public class Example {
	public static void main(final String[] args) {
		final String className = Thread.currentThread().getStackTrace()[1].getClassName();
		System.out.println(className);
	}
}

output


Example

Methodization

Util.java


public class Util {
	/**
	 *Gets the name of the running class.
	 * @return class name
	 */
	public static String getClassName() {
		return Thread.currentThread().getStackTrace()[2].getClassName();
	}
}

UtilTest.java


public class UtilTest {
	public static void main(final String[] args) {
		final String className = Util.getClassName();
		System.out.println(className);
	}
}

output


UtilTest

Get method name

Get from run-time class

Example.java


public class Example {
	public static void main(final String[] args) {
		final String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
		System.out.println(methodName);
	}
}

output


main

Get from stack trace

Example.java


public class Example {
	public static void main(final String[] args) {
		final String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
		System.out.println(methodName);
	}
}

output


main

Methodization

Util.java


public class Util {
	/**
	 *Gets the name of the method being executed.
	 * @return method name
	 */
	public static String getMethodName() {
		return Thread.currentThread().getStackTrace()[2].getMethodName();
	}
}

UtilTest.java


public class UtilTest {
	public static void main(final String[] args) {
		final String methodName = Util.getMethodName();
		System.out.println(methodName);
	}
}

output


main

Recommended Posts

How to get the class name / method name running in Java
How to get the date in java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to get Class from Element in Java
How to get the absolute path of a directory running in Java
How to name variables in Java
[Java] How to use the File class
[Java] How to use the HashMap class
[Java] How to use the toString () method
[Java] How to get the current directory
[Processing × Java] How to use the class
[Java] How to use the Calendar class
How to get the length of an audio file in java
How to get keycloak credentials in interceptor class
When you get lost in the class name
How to use the replace () method (Java Silver)
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
[Java] How to get to the front of a specific string using the String class
How to get the value after "_" in Windows batch like Java -version
How to reference a column when overriding the column name method in ActiveRecord
How to create your own annotation in Java and get the value
How to get Excel sheet name list in Java (POI vs SAX)
How to use java class
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
[Java] How to omit the private constructor in Lombok
Get the name of the test case in the JUnit test class
[Java] How to extract the file name from the path
[Ruby] Method to easily get the receiver type .class
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
Why the get method is needed in the Calendar class
graphql-ruby: How to get the name of query or mutation in controller Note
How to use the link_to method
How to use the getter / setter method (in object orientation)
How to use Java HttpClient (Get)
How to use the include? method
How to use the form_with method
How to disassemble Java class files
[IOS] How to get the table name from AWS DynamoDB
[Java] How to use join method
How to use the wrapper class
[Java] How to get the key and value stored in Map by iterative processing
[Android, Java] Convenient method to calculate the difference in days
How to learn JAVA in 7 days
How to get parameters in Spark
How to decompile java class files
Create a method to return the tax rate in Java
[Java] How to use LinkedHashMap class
How to use class methods [Java]
How to use classes in Java?
How to derive the last day of the month in Java
How to switch Java in the OpenJDK era on Mac
[Java] How to use Math class
How to concatenate strings in java
Call the super method in Java
[Java] Get date information 10 days later using milliseconds in the Date class
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to store the information entered in textarea in a variable in the method
How to solve the unknown error when using slf4j in Java