以全大写形式声明记录器并将其定为最终版本有什么区别吗?

发布于 2024-09-25 03:24:50 字数 269 浏览 0 评论 0原文

有什么理由这样做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

開玄 2024-10-02 03:24:50

常量全部大写,这是从 C/C++ 世界继承下来的约定。由于在 Java 中,最接近的常量是static final,因此这些成员的命名通常类似。

在这种情况下,区别可能并不那么重要,因为记录器对象在语义上不是常量。尽管如此,仍然建议将它们声明为static,以确保每个类都有一个实例,并且您可能需要将它们声明为final,以确保对对象的引用以后无法更改(并将这一事实传达给代码的读者)。

更新 @Moncader 的评论:

  1. static 相当于 C++ (class) static - 只是它没有多大意义在 C++ 中将常量声明为非静态,在 Java 中也没有意义。

  2. 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 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.

温柔嚣张 2024-10-02 03:24:50

为什么要把它定为最终版本?

因为这样你就可以确定没有人可以更改当前的记录器。

为什么是 LOGGER 而不是 Logger?

这是 Java 约定。常量以大写形式命名。

声明的类常量和 ANSI 常量的变量名称应全部大写,单词之间用下划线(“_”)分隔。 (为了便于调试,应避免使用 ANSI 常量。)


资源:

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 :

丢了幸福的猪 2024-10-02 03:24:50

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.

渔村楼浪 2024-10-02 03:24:50

一些(不是全部)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.

柠檬色的秋千 2024-10-02 03:24:50

没有功能上的区别。在第一个示例中,记录器是一个常量,它永远不会改变。常量全部大写是 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.

我纯我任性 2024-10-02 03:24:50

如果您使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文