3

what difference does it make in a Java Interface to declare the method signature as final and non-final?

int setName(String name);

int setName(final String name);
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • 2
    @Keoki while this is true, it kinda defeats the purpose of Stack Overflow. – slhck Jun 19 '11 at 08:30
  • 1
    You could rephrase your question as you are using a final parameter. – Snicolas Jun 19 '11 at 08:31
  • 2
    Related: [Final arguments in interface methods - what's the point?](http://stackoverflow.com/questions/5380177/final-arguments-in-interface-methods-whats-the-point) – slhck Jun 19 '11 at 08:38

3 Answers3

10

Section 8.4.1 of the Java Language specification allows the parameters in any method declaration (and that includes the ones in interfaces) to be declared final. However, since this does not influence the method's signature, declaring a parameter of an abstract function as final has no effect. Since all methods in an interface are implicitely abstract, both variants are equivalent.

phihag
  • 278,196
  • 72
  • 453
  • 469
2

The most complete answer I could find on google is this one.

It's nice they mention the link with using anonymous inner classes as it's a strong use case for final parameters.

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173
1

A couple of incidental differences

  • It can be used by the IDE as a hint. e.g. when you get the IDE to auto-generate your implemented methods, it will make the parameters final by default in the same way it will re-use the same parameter names even though these are not part of the signature either.

  • the modifier final is available via reflection and could be used by a framework for some implied purpose.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130