How can one get the name of the class from a static method in that class. For example
public class MyClass {
public static String getClassName() {
String name = ????; // what goes here so the string "MyClass" is returned
return name;
}
}
To put it in context, I actually want to return the class name as part of a message in an exception.
You could do something really sweet by using JNI like this:
MyObject.java:
then:
then:
MyObject.c:
Then compile the C up into a shared library called
libclassname.so
and run the java!*chuckle
A refactoring-safe, cut&paste-safe solution that avoids the definition of ad-hoc classes below.
Write a static method that recover the class name having care to include the class name in the method name:
then recall it in your static method:
Refactoring safety is given by avoiding the use of strings, cut&paste safety is granted because if you cut&paste the caller method you won't find the getMyClassName() in the target "MyClass2" class, so you will be forced to redefine and update it.
Since the question Something like `this.class` instead of `ClassName.class`? is marked as a duplicate for this one (which is arguable because that question is about the class rather than class name), I'm posting the answer here:
It is important to define
thisClass
as private because:1) it must not be inherited: derived classes must either define their own
thisClass
or produce an error message2) references from other classes should be done as
ClassName.class
rather thanClassName.thisClass
.With
thisClass
defined, access to the class name becomes:If you are using reflection, you can get the Method object and then:
To get the Method itself, you can probably use:
Abuse the SecurityManager
Or, if not set, use an inner class that extends it (example below shamefully copied from Real's HowTo):
So, we have a situation when we need to statically get class object or a class full/simple name without an explicit usage of
MyClass.class
syntax.It can be really handy in some cases, e.g. logger instance for the kotlin upper-level functions (in this case kotlin creates a static Java class not accessible from the kotlin code).
We have a few different variants for getting this info:
new Object(){}.getClass().getEnclosingClass();
noted by Tom Hawtin - tackline
getClassContext()[0].getName();
from theSecurityManager
noted by Christoffer
new Throwable().getStackTrace()[0].getClassName();
by count ludwig
Thread.currentThread().getStackTrace()[1].getClassName();
from Keksi
and finally awesome
MethodHandles.lookup().lookupClass();
from Rein
I've prepared a jmh benchmark for all variants and results are:
Conclusions
Available only since Java 7 and Android API 26!
If you need it in many places and don't want your bytecode to bloat due to tons of anonymous classes –
SecurityManager
is your friend (third best option).But you can't just call
getClassContext()
– it's protected in theSecurityManager
class. You will need some helper class like this:getStackTrace()
from exception or theThread.currentThread()
. Very inefficient and can return only the class name as aString
, not theClass<*>
instance.P.S.
If you want to create a logger instance for static kotlin utils (like me :), you can use this helper:
Usage example: