Reputation: 673
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.
Upvotes: 0
Views: 1544
Reputation: 116266
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:
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.
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.
Upvotes: 5
Reputation: 274592
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.
Upvotes: 0
Reputation: 3388
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.
Upvotes: 1
Reputation:
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.
Upvotes: 0
Reputation: 308763
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.
Upvotes: 2
Reputation: 93167
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 :
Upvotes: 3