Is there a way in Java to find out which class I'm in without referencing an object? I need the current class name from a static point. Here is an example:
public class MyClass {
// is this possible?
private static final String myClassName = ???;
// not like this
void someMethod(){
String className = this.getClass();
}
// or this
private static final String myClassName = MyClass.class;
}
The overall problem is logging. The logger of the project I'm working on needs the class name and is supposed to be declared like this:
private static final Logger LOGGER = LogHelper.getLogger(MyClass.class);
If there is a more generic way, it would be easier to copy the logger.
I need this code in multiple classes, and it would be nice to just copy the code instead of always replacing "MyClass.class" every time.
Unfortunately, different approaches wouldn't be compatible with the conventions of this project.