Monthly Archives: August 2013

How to obtain class name within it’s sources in Java 7

When we implement a class there is frequently need to obtain class name which we currently develop. This is especially true when we implementing logging.
Lets look on below short example:

private static final Logger LOG = Logger.getLogger(ClazzName.class, Logger.NF);

The result as we see is that we need to every time on every class which is supposed to do logging enter a class name.
This is cumbersome and more over error prone. Solution thanks to introduction in Java 7 method handles is fairy simple, let’s look
and next example:

private static final Logger LOG = Logger.getLogger(MethodHandles.lookup().lookupClass(), Logger.NF);
Advertisements