This might be because of a "hidden" feature of the Java language, generic types 
are preserved in anonymous classes...

Sent from my iPhone

> On Mar 4, 2015, at 7:41 PM, Aditya <[email protected]> wrote:
> 
> Nice.
> 
> On Wed, Mar 4, 2015 at 4:06 PM, Chris Westin <[email protected]>
> wrote:
> 
>> In some files I've been working in, I just noticed that the loggers aren't
>> static. (This is a different topic than the previous one re making them
>> private).
>> 
>> I set out to make them private final static, and discovered the reason:
>> they're generics. There's no way to create class literals for them. That
>> is, you can't do this:
>> 
>> public class Foo<T> {
>>  private final static org.slf4j.Logger logger =
>> org.slf4j.LoggerFactory.getLogger(Foo.class);
>> 
>> Foo<T>.class doesn't work either. Searches reveal that there's no way to
>> create a class literal
>> for a generic. However, we are certainly not the only ones struggling with
>> this, and someone out
>> there came up with this solution that does work:
>> 
>> public class Foo<T> {
>>  private final static org.slf4j.Logger logger =
>> org.slf4j.LoggerFactory.getLogger(new Object()
>> {}.getClass().getEnclosingClass());
>> 
>> I'll start using this whenever I see the old pattern in a generic class
>> (which was to not declare the logger static, and to use this.getClass()). I
>> figure it's better to have some throwaway objects once at class load time
>> rather than having every instance have it's own (identical) logger.
>> 
>> Chris
>> 

Reply via email to