views:

97

answers:

6

Is there any reason to do this:

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

instead of this?

private static Logger logger = LoggerFactory.getLogger(Main.class);

I don't undertstand what the syntactical benefit is of one over the other. Both seem to work fine.

+3  A: 

Why make it final ?

Because this way you're sure that no-one can change the current logger.

Why LOGGER and not Logger ?

That is a Java convention. Constants are named in Uppercase.

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)


Resources :

Colin Hebert
+4  A: 

It is a convention inherited from the C/C++ world that constants are named all uppercase. Since in Java, the closest equivalent of constant is static final, these members are often named similarly.

In this case, the distinction may not be so important, since logger objects are semantically not constants. Nevertheless, it is still recommended to declare them static to ensure you have a single instance per class, and you may want to declare them final to ensure that the reference to the object can't be changed later (and to communicate this fact to readers of your code).

Update to @Moncader's comment:

  1. static is equivalent to C++ (class) static - just as well as it does not make much sense in C++ to declare your constants as non-static, it does not make sense in Java either.

  2. final is not a direct equivalent of C++ const. It means that the reference in question can not be changed. However, it does not prevent changing the object referred to! So it only works as const if it protects a primitive value (e.g. int) or a reference to an immutable object (e.g. String). To stay with our current topic, consider

    private static final Logger logger = LoggerFactory.getLogger(Main.class); ... logger.setLevel(Level.INFO); logger.addAppender(...); logger.setAdditivity(false);

All of these calls are obviously changing the state of the logger, even though it is declared final. That's what I meant above by stating that logger objects are semantically not constants.

Péter Török
static has nothing to do with C/C++ const and has absolutely no relation to them. final is a direct translation of const. static is completely different.
Moncader
@Moncader, not fully correct - see my update.
Péter Török
+2  A: 

final means that you can't change the reference to point to something else.

Upper case is just a coding convention that suggests to a reader that this is a static class constant, especially if you preface it with the short class name when referring to it (e.g., Foo.LOGGER.info("message");) See the 1999 Sun Java coding conventions.

duffymo
A: 

There's no functional difference. In your first example, the logger is a constant, it can never change. Using all caps for constants is a Java convention. In your second example, the logger is static but not final, so it can be changed by caller code. You probably don't want that, so I would use the first option.

Jorn
+1  A: 

Some (not all) Java VMs optimize final constants which make access to your logger quicker. Therefore, if a value anywhere in your program will never change, make it final.

LOGGER is just what Java people do for constants. If you really wanted to make it lower case, there is no difference in terms of performance or anything program-wise.

Moncader
I believe the JVM is able to see anyway that the reference is never changed, so `final` may not make a difference from this point of view (but then again, I am not a JVM expert).
Péter Török
A: 

If you use a code checking tool like PMD, then you will see that a static final logger (lower case) will be flagged as a warning.

There is more on the upper vs lowercase logger debate in this SO question, which I asked previously.

dogbane