开发者

why to avoid constant folding in Java? When?

开发者 https://www.devze.com 2023-02-04 17:34 出处:网络
I saw some codes in slf4j as show below.I don\'t know why to avoid constant folding in here. Is it necessary to do that? or just best practice. what\'s the benefit of doing this?

I saw some codes in slf4j as show below. I don't know why to avoid constant folding in here. Is it necessary to do that? or just best practice. what's the benefit of doing this?

Thanks.

/**
  * Declare the version of the SLF4J API th开发者_运维知识库is 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**


In the particular case where you are releasing a library, you often don't have control of the final version of the logging library that is eventually linked in at the end. For example you are using version 1.6, and the application that is using your library might use 1.6.1 to get a bugfix. Since it's only a point release the API should be compatible, but if your library inspects the SLF4J version it should show 1.6.1 not 1.6.

If the constant is inlined you will see 1.6 (since it's copied into your class file) even if the library is upgraded after the fact.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号