Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Runtime Class and Method information

DZone's Guide to

Runtime Class and Method information

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Here is a small snippet that can be used to find the name of the currently executing method in Java.

Throwable t = new Throwable(); 
StackTraceElement[] elements = t.getStackTrace();
System.out.println(elements[0].getMethodName());

Create a Throwable object in your method. During the creation of a Throwable instance, the JVM stores information of the currently executing method on the stack trace. Extract the stack trace and print the name of the zeroth element to get your method name.

Here is another small snippet to find the name of the class at runtime.

Thread.currentThread().getStackTrace()[1].getClassName()


Tiny little things, aren't they!

Happy Programming :)
Signing Off

From http://mycodefixes.blogspot.com/2011/03/runtime-class-and-method-information.html

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}