在Java中,有一种惯例,将每个变量(局部变量或类)声明为final参数(如果它们确实是final的话)。

虽然这会使代码更加冗长,但这有助于容易阅读/掌握代码,也可以防止错误,因为意图被清晰地标记出来。

你对此有何看法?


当前回答

我从来没有遇到过在变量上有最后一个关键字就能阻止我犯错误的情况,所以目前我认为这是一种巨大的浪费时间。

除非有真正的理由这样做(比如你想要明确指出变量是final的),否则我宁愿不这样做,因为我发现这样做会使代码的可读性降低。

但是,如果您没有发现它会使代码更难阅读或更长时间地编写,那么无论如何都可以使用它。

编辑:作为一个澄清(并试图赢回反对票),我不是说不要将常量标记为最终值,我是说不要做这样的事情:

public String doSomething() {
  final String first = someReallyComplicatedExpressionToGetTheString();
  final String second = anotherReallyComplicatedExpressionToGetAnother();

  return first+second;
}

在我看来,这只是让代码更难阅读。

同样值得记住的是,所有final的作用都是防止你重新分配变量,它并没有使它成为不可变或类似的东西。

其他回答

为事件监听器使用匿名本地类等是Java中的常见模式。 final关键字最常见的用法是确保偶数侦听器可以访问作用域内的变量。

但是,如果您发现自己被要求在代码中放入大量的最终语句。这可能是你做错事的好暗示。

上面的文章给出了这样一个例子:

public void doSomething(int i, int j) {
    final int n = i + j; // must be declared final

    Comparator comp = new Comparator() {
        public int compare(Object left, Object right) {
            return n; // return copy of a local variable
        }
    };
}

I have to read a lot of code for my job. Missing final on instance variables is one of the top things to annoy me and makes understanding the code unnecessarily difficult. For my money, final on local variables causes more clutter than clarity. The language should have been designed to make that the default, but we have to live with the mistake. Sometimes it is useful particularly with loops and definite assignment with an if-else tree, but mostly it tends to indicate your method is too complicated.

当在Java中与变量一起使用时,Final提供了c++中常量的替代品。所以当final和static被用于一个变量时,它就变成了不可变的。同时让迁移的c++程序员非常开心;-)

当与引用变量一起使用时,它不允许您重新引用对象,尽管对象可以被操作。

当final与方法一起使用时,它不允许子类重写该方法。

一旦用法非常清楚,就应该谨慎使用。这主要取决于设计,因为在方法上使用final不利于多态性。

只有当你非常确定变量的值不会/不应该被改变时,才应该将它用于变量。还要确保您遵循SUN鼓励的编码约定。int COLOR_RED = 1;(大写字母以下划线分隔)

对于引用变量,只在需要对特定对象的不可变引用时使用它。

关于可读性部分,在使用最终修饰语时,注释起着非常重要的作用。

另一个注意事项是,许多人将final混淆为实例变量的内容不能更改,而不是引用不能更改。

对于将所有可能的变量都声明为final,我非常武断。这包括方法参数、局部变量,以及很少的值对象字段。我有三个在任何地方声明最终变量的主要原因:

Declaring Intention: By declaring a final variable, I am stating that this variable is meant to be written to only once. It's a subtle hint to other developers, and a big hint to the compiler. Enforcing Single-use Variables: I believe in the idea that each variable should have only one purpose in life. By giving each variable only one purpose, you reduce the time it takes to grok the purpose of that particular variable while debugging. Allows for Optimization: I know that the compiler used to have performance enhancement tricks which relied specifically on the immutability of a variable reference. I like to think some of these old performance tricks (or new ones) will be used by the compiler.

然而,我确实认为final类和方法远不如final变量引用有用。最后一个关键字,当与这些声明一起使用时,只是为自动化测试和以您从未预料到的方式使用代码提供了障碍。