简体   繁体   English

为什么Netbeans建议变量最终

[英]Why Netbeans suggests making variable final

So i have downloaded new Netbeans 8 version some time ago. 所以我前段时间下载了新的Netbeans 8版本。 In this new version everytime when i declare private variable and assign to it value only once netbeans suggests me to make field final because is never changed. 在这个新版本中,每当我声明private变量并仅为其分配值时, netbeans建议我将字段设为最终,因为永远不会更改。

Ok I understand what final variable is but **whats the advantage in declaring private final int x=3; 好的,我明白final变量是什么,**在声明private final int x=3;什么优势private final int x=3; variable aka constant over private int x=3; 变量aka常量超过private int x=3; and never change it ** 永远不要改变它**

i thought about 我想过

  1. Concurrency ? 并发? - But this doesnt make sense for me if value of non-final variable remains the same whole time - 但是如果非最终变量的值始终保持不变,这对我来说没有意义
  2. Speed ?- I think this may be the reason(some caching ?) but im not sure 速度? - 我认为这可能是原因(一些缓存?)但我不确定

Any clarification will be appreciated 任何澄清将不胜感激

Netbeans detects that the variable is actually probably a constant, and suggest for you to declare it as such. Netbeans检测到变量实际上可能是常量,并建议您将其声明为常量。 This has the advantage of preventing future bugs where this variable is accidentally modified. 这样做的好处是可以防止意外修改此变量的未来错误。

Also the final also documents the intention of making this variable a constant, together with the usual notation of defining the constant with all uppercase, which helps understand it's purpose in the program to other developers maintaining the code in the future. 最后,最后还记录了使这个变量成为常量的意图,以及通常用大写字母定义常量的符号,这有助于理解程序中其他开发人员在未来维护代码的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM