以全大写形式声明记录器并将其定为最终版本有什么区别吗?
有什么理由这样做:
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
而不是这样做?
private static Logger logger = LoggerFactory.getLogger(Main.class);
我不明白其中一种相对于另一种在语法上有什么好处。两者似乎都工作正常。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
常量全部大写,这是从 C/C++ 世界继承下来的约定。由于在 Java 中,最接近的常量是
static final
,因此这些成员的命名通常类似。在这种情况下,区别可能并不那么重要,因为记录器对象在语义上不是常量。尽管如此,仍然建议将它们声明为
static
,以确保每个类都有一个实例,并且您可能需要将它们声明为final
,以确保对对象的引用以后无法更改(并将这一事实传达给代码的读者)。更新 @Moncader 的评论:
static
相当于 C++ (class)static
- 只是它没有多大意义在 C++ 中将常量声明为非静态,在 Java 中也没有意义。final
不是 C++const
的直接等价物。这意味着无法更改相关参考。但是,它并不阻止更改所引用的对象!因此,只有当它保护原始值(例如int
)或对不可变对象的引用(例如String
)时,它才作为const起作用。为了继续我们当前的主题,请考虑私有静态最终Logger logger = LoggerFactory.getLogger(Main.class);
...
logger.setLevel(Level.INFO);
logger.addAppender(...);
logger.setAdditivity(false);
所有这些调用显然都会改变记录器的状态,即使它被声明为
final
。这就是我上面所说的记录器对象在语义上不是常量的意思。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 themfinal
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, considerprivate 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.为什么要把它定为最终版本?
因为这样你就可以确定没有人可以更改当前的记录器。
为什么是 LOGGER 而不是 Logger?
这是 Java 约定。常量以大写形式命名。
资源:
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.
Resources :
final
意味着您无法更改引用以指向其他内容。大写只是一种编码约定,它向读者表明这是一个静态类常量,特别是当您在引用它时使用短类名作为前缀时(例如,
Foo.LOGGER.info("message") ;
) 请参阅 1999 Sun Java 编码约定 。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.一些(不是全部)Java VM 优化了最终常量,可以更快地访问记录器。因此,如果程序中任何位置的值永远不会改变,请将其设为最终值。
LOGGER 正是 Java 人们对常量所做的事情。如果你真的想让它小写,那么在性能或任何程序方面都没有区别。
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.
没有功能上的区别。在第一个示例中,记录器是一个常量,它永远不会改变。常量全部大写是 Java 的惯例。
在第二个示例中,记录器是静态的但不是最终的,因此可以通过调用者代码更改它。您可能不希望这样,所以我会使用第一个选项。
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.
如果您使用PMD之类的代码检查工具,那么您将看到
静态最终记录器< /code>(小写)将被标记为警告。
此中有更多关于大写与小写记录器的争论 这是我之前问过的问题。
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.