-1

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.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Ruik
  • 1,222
  • 1
  • 20
  • 36
  • 5
    Your question may in fact be an [XY Problem](http://mywiki.wooledge.org/XyProblem) where you ask "how do I fix this code problem" when the best solution is to use a different approach entirely. Consider telling us the overall problem that you're trying to solve rather than how you're currently trying to solve it. – Hovercraft Full Of Eels Sep 29 '16 at 15:57
  • 3
    To use a static reference you need the class name *anyway*. What are you trying to accomplish with this? – Makoto Sep 29 '16 at 15:58
  • If you're already in a static block, then the class name is whatever class you're writing it in. Strictly speaking there isn't inheritence of static members/methods. – Rogue Sep 29 '16 at 16:01
  • 2
    In general the answer is "no"; creating loggers in that fashion is the best and simplest way, and anything overly complicated will only hurt your efforts. – Makoto Sep 29 '16 at 16:10
  • http://stackoverflow.com/questions/80692/java-logger-that-automatically-determines-callers-class-name – Sotirios Delimanolis Sep 29 '16 at 16:24
  • http://stackoverflow.com/questions/936684/getting-the-class-name-from-a-static-method-in-java – Sotirios Delimanolis Sep 29 '16 at 16:25
  • http://stackoverflow.com/questions/5733427/why-isnt-getclass-available-as-a-static-method – Sotirios Delimanolis Sep 29 '16 at 16:25

2 Answers2

1

Some compilers like eclipse lets you do it, for exsamples look this page:

http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-code-templates/

and use this in your new template:

private static final Logger LOGGER = LogHelper.getLogger(${primary_type_name}.class
0

To put it simply, there likely isn't a simpler or more generic approach to doing this. While I'm uncertain of the exact logger library you're using, the main fact remains that you must supply the class that you want to log to the logger.

It does mean extra typing, but there's no realistic approach that is simpler, easier to understand, or safer - anything that's overly complex and uses a lot of reflection to extract the class name would need to be tested to ensure that it always returns the correct class, which is substantially more effort than simply using MyClass.class.

Makoto
  • 104,088
  • 27
  • 192
  • 230