what's the point?

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, eg:

public interface Foo {
    public void foo(int bar, final int baz);
}

public class FooImpl implements Foo {

    @Override
    public void foo(final int bar, int baz) {
        ...
    }
}

In the above example, bar and baz has the opposite final definitions in the class VS the interface.

In the same fashion, no final restrictions are enforced when one class method extends another, either abstract or not.

While final has some practical value inside the class method body, is there any point specifying final for interface method parameters?


It doesn't seem like it. According to the Java Language Specification 4.12.4:

Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

However, a final modifier on a method parameter is not mentioned in the rules for matching signatures of overridden methods, and it has no effect on the caller, only within the body of an implementation. Also, as noted by Robin in a comment, the final modifier on a method parameter has no effect on the generated byte code. (This is not true for other uses of final .)


Some IDEs will copy the signature of the abstract/interface method when inserting an implementing method in a sub class.

I don't believe it makes any difference to the compiler.


Final annotations of method parameters are always only relevant to the method implementation never to the caller. Therefore, there is no real reason to use them in interface method signatures. Unless you want to follow the same consistent coding standard, which requires final method parameters, in all method signatures. Then it is nice to be able to do so.

链接地址: http://www.djcxy.com/p/20848.html

上一篇: 方法参数中的最终关键字

下一篇: 重点是什么?