4

So I can fully understand why you may want to make your class final. Imagine String class not being final, and you have a library, in which you have a method that takes a String as an argument and depends on the length() method for some operation, but someone extends the String class and overrides the length to return -1 always..

But I can not figure out why anyone would ever declare a parameter of a method final. Java is pass by value anyway, so if the parameter is a primitive, it is already final (in a sense).. And if you are passing a Reference Type does it guarantee anything to the caller of the method? I do not think so..

void foo(final List<Object> o) {
    o.add(new Object());
} // oops?

So final prevents the following:

void foo(final List<Object> o) {
    o = new ArrayList();
}

But why would I ever want to do this anyway? I can always create a new variable, and it does not assign a new Object to the caller 's variable anyway? (I mean, I can not change what the callers reference refers to anyway, even if the parameter was not declared final..)

My question is, what is the purpose of a final parameter in a method declaration? Is it for the caller? Does it guarantee anything?

I also find strange that the following will compile fine:

public interface MyInterface {
    void foo(final Object o);
}

class MyImpl implements MyInterface {
    @Override
    public void foo(Object o) { // final in interface, not final in impl..
    }
}

but maybe it will make sense once I understand the main question I have.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • http://stackoverflow.com/a/20795203/1008222 – Mithun Sasidharan May 07 '17 at 06:26
  • 2
    All the people that hurried to respond or flag as dup - you guys have light finger on the trigger... The OP shows perfect understanding of the semantics of the final keyword next to an argument, he asks for the *motivation* because it seems like a redundant addition to Java! – Nir Alfasi May 07 '17 at 06:41
  • And I believe that the only possible answer is to enforce "good practice" (not re-assign an argument). Other than that I don't see any added value in making an argument final. – Nir Alfasi May 07 '17 at 06:51
  • 1
    @alfasin You could read the dupe, instead of writing these comments. The linked question also says that you've wrote (e.g. [Why should I use the keyword "final" on a method parameter in Java?](//stackoverflow.com/a/10380512)). – Tom May 07 '17 at 06:56
  • An interesting (related) discussion can be found here: http://wiki.c2.com/?JavaFinalArguments – Nir Alfasi May 07 '17 at 07:38
  • SE has discussed this as well: [Excessive use "final" keyword in Java](//softwareengineering.stackexchange.com/q/98691) (also check the linked dupe there). – Tom May 07 '17 at 08:34

2 Answers2

0

It doesn't guarantee anything to the caller either way (regardless whether the argument is a primitive type or a reference type).

A method argument is a local variable whose value is initialized by the caller of the method. Making it final means the body of the method cannot assign a new value to that variable locally, so it only makes a different within the body of the method (since assigning a new value to such a variable within the method body would make no different for the caller of the method).

It ensures that any code in the method's body that uses the value of the argument would use the original value passed by the caller and not some different value that was assigned to it within the method body.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • The OP understands it, his question is what's the motivation. Since Java is "pass by val" and the parameter is a copy of the original reference (assuming it's an object) re-assigning won't matter for the caller - so what's the point in allowing an argument to be final. – Nir Alfasi May 07 '17 at 06:42
  • 2
    There isn't really a difference between parameters and local variables, because Java is PbV. So the question can be extended to "why make local variables final", which has been asked and answered many times. – Andy Turner May 07 '17 at 07:07
0

Sometimes, it helps the fellow programmers or maintenars that they should not change the value of that variable.

Ex: Imagine a method taking a string parameter. Imagine that it a very long method having 1000 lines of code. If the parameter is not marked as final, the second programmer see an opportunity of changing the value in it. The first programmer resumes the work on the method while not having the knowledge that the variable is assigned a new value. Making it final will fully assure the first programmers intention to be passed on to second.

Apart from this, if you need to use this variable in any of anonymous class in that method, you can't use it unless it is final.

Hope this helps.

Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46
  • 3
    Though re-assigning an argument is a bad practice - it does not have any effect outside of the method. If you have a 1000 lines long method your concerns should be about refactoring it, not making an argument final. – Nir Alfasi May 07 '17 at 06:48
  • Agree. But question is about final and final variables helps in such a situations. And you can't say that a 1000 line method can't exist. It depends. And as i said, its for information purpose for second programmer to not change the variable on the long course of application life. – Shailesh Pratapwar May 07 '17 at 07:10