Over a million developers have joined DZone.

Accessing the class object in a static context

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

I often need to access the current class, for example for logging purposes:

public class Foo {
  private Logger logger = LoggerFactory.getLogger(getClass());
}

However, this won’t work in a static context, since you don’t have any this object to call getClass() on:

public class Foo {
  static private Logger logger = LoggerFactory.getLogger(Foo.class);
}

It’s always bothered me to have to copy/paste this line and then remember to replace the name of the class, so here is one way to make this snippet more generic:

public class ClassUtil extends SecurityManager {
 
  public static Class getCurrentClass() {
    return getClassContext()[1];
  }
 
}

Which you use as follows:

public class Foo {
  private static Logger logger = LoggerFactory.getLogger(
      new ClassUtil().getCurrentClass());
}

A somewhat less hacky and cheaper approach is to instantiate an anonymous class in order to materialize a this object:

private static Class thisClass =
    new Object() { }.getClass().getEnclosingClass();
private static Logger logger = LoggerFactory.getLogger(thisClass);

Can you think of any other way?

From http://beust.com/weblog/2011/07/15/accessing-the-class-object-in-a-static-context

The Java Zone is brought to you in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.

Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}