java :Why the Local variable should be declared final

标签 java final pmd

<分区>

Possible Duplicate:
Is there any performance reason to declare method parameters final in Java?
Why would one mark local variables and method parameters as “final” in Java?

我正在使用 PMD 查看代码违规情况。

在 webService 方法中,我有下面的代码

public ServiceRequest getData()
{
Status status = new Status();
// code
}

PMD 给我的建议是,这个局部变量状态可以声明为 final。

我的问题是,将其定为 final 会导致任何性能改进,或者如果没有,代码可以获得什么好处?

最佳答案

摘自以下文章:http://www.javapractices.com/topic/TopicAction.do?Id=23

  • 清楚地传达您的意图
  • 允许编译器和虚拟机执行较小的优化
  • 清楚地标记出行为较简单的项目 - final 表示,“如果您正在寻找复杂性,您将不会在这里找到它。”

这个问题也有讨论:Can excessive use of final hurt more than do good?

关于java :Why the Local variable should be declared final,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9195752/

相关文章:

java - 在 HashMap 的数组列表中排序

java - Java 中的 Cookie 最大年龄

java - 如何处理抛出检查异常的静态最终字段初始化程序

maven - 如何查找 pmd 规则集的 xml 名称

java - hibernate : include configuration files in WEB-INF in tests

java - Android打开失败: EACCES (Permission denied)

java - 等价于 Java 中的 const(C++)

java - 避免在时间戳上使用 java.lang.ThreadGroup

java - 确保具有特定注释的字段为 'private'