Determine class name in Java
This post will discuss how to determine the class name of the underlying class of objects in Java.
1. Class’s get*Name() methods
The simplest way is to call the getClass() method that returns the class’s name or interface represented by an object that is not an array. We can also use getSimpleName() or getCanonicalName(), which returns the simple name (as in source code) and canonical name of the underlying class, respectively. getTypeName() is a recent addition to JDK in Java SE 8 which internally calls getClass().
⮚ For class objects:
|
1 2 3 4 5 6 7 |
System.out.println(this.getClass().getSimpleName()); // prints <class name> // The following prints <package name> + <class name> System.out.println(this.getClass().getName()); System.out.println(this.getClass().getCanonicalName()); System.out.println(this.getClass().getTypeName()); |
⮚ For Reference Types:
|
1 2 3 4 5 |
String obj = "A"; System.out.println(obj.getClass().getSimpleName()); // String System.out.println(obj.getClass().getName()); // java.lang.String System.out.println(obj.getClass().getCanonicalName()); // java.lang.String System.out.println(obj.getClass().getTypeName()); // java.lang.String |
⮚ For Arrays:
|
1 2 3 4 5 |
String[] array = { "1", "B", "C" }; System.out.println(array.getClass().getSimpleName()); // String[] System.out.println(array.getClass().getCanonicalName()); // java.lang.String[] System.out.println(array.getClass().getTypeName()); // java.lang.String[] |
2. Using Class.getEnclosingClass() method
For the anonymous inner class, we need to call Class.getEnclosingClass() that returns the immediately enclosing class of the underlying class.
|
1 2 3 4 5 6 7 8 9 10 |
// determine class name in Java public void getClassName() { String className = new Object() {} .getClass() .getEnclosingClass() .getName(); System.out.println("Current Class is " + className); } |
We can also define an inner class to get the class name of the enclosing class, as shown below:
|
1 2 3 4 5 6 7 8 9 10 |
// determine class name in Java public void getClassName() { class Dummy {}; String className = Dummy.class .getEnclosingClass() .getName(); System.out.println(className); } |
3. Using Stack Trace
We can get an array of stack trace elements representing the stack trace pertaining to a throwable. The first element of the returned array of stack trace contains the current method’s stack frame (which also stores class information).
|
1 2 3 4 5 6 7 8 9 |
// determine class name in Java public void getClassName() { String className = new Throwable() // or new `Exception()` .getStackTrace()[0] .getClassName(); System.out.println("Current Class is " + className); } |
The stack trace of a current thread also returns an array of stack trace elements representing the stack dump of this thread. We can find the current method’s stack frame (containing class information) at index 1 of the array.
|
1 2 3 4 5 6 7 8 9 |
// determine class name in Java public void getClassName() { String className = Thread.currentThread() .getStackTrace()[1] .getClassName(); System.out.println("Current Class is " + className); } |
Please note that this method is not safe to use as few virtual machines may omit one or more stack frames from the stack trace under special circumstances.
That’s all about checking class names in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)