为什么要避免在Java中不断折叠?什么时候?

问题描述 投票:9回答:1

我在slf4j中看到了一些代码,如下所示。我不知道为什么要避免在这里不断折叠。有必要这样做吗?或者只是最好的做法。这样做有什么好处?

谢谢。

/**
  * Declare the version of the SLF4J API this implementation is compiled against. 
  * The value of this field is usually modified with each release. 
  */
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.6";  // !final**
java compiler-construction slf4j constantfolding
1个回答
6
投票

在您发布库的特定情况下,您通常无法控制最终在最终链接的日志库的最终版本。例如,您使用的是版本1.6,使用您的库的应用程序可能会使用1.6.1来获取错误修复程序。由于它只是一个点发布,API应该兼容,但如果你的库检查SLF4J版本它应该显示1.6.1而不是1.6。

如果内联常量,您将看到1.6(因为它被复制到您的类文件中),即使该库在事后已升级。

© www.soinside.com 2019 - 2024. All rights reserved.