What is the purpose of declaring method parameters final?

So, I can fully understand why you can make your class final

. Imagine the String class is not final and you have a library where you have a method that takes a string as an argument and depends on length () for some operation, but someone extends the String class and overrides the length to return -1 always.

But I can't figure out why anyone was declaring a parameter of the final method. Anyway, Java is passed by value, so if the parameter is primitive it is already final (in a sense). And if you pass in a reference type, does it guarantee anything to the calling 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 do this at all? I can always create a new variable and it still doesn't assign the new object to the client variable? (I mean, I cannot change what the caller reference is referenced anyway, even if the parameter was not declared final ..)

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

I'm also surprised the following will compile just 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 basic question I have.

+3


source to share


2 answers


Sometimes, this helps programmers or professionals not to change the value of this variable.

Ex: Imagine a method that takes a string parameter. Imagine this is a very long method with 1000 lines of code. If the parameter is not marked as final, the second programmer sees an opportunity to change the value in it. The first programmer resumes working on this method, unaware that a new value is being assigned to the variable. The final ending will fully ensure that the first intent of the programmers is passed on to the second.



Also, if you need to use this variable in any of the anonymous class in this method, you cannot use it unless it is final.

Hope it helps.

0


source


This does not guarantee anything to the caller in any way (regardless of 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. Ending means that the body of a method cannot assign a new value to this variable locally , so it only distinguishes within the body of a method (since assigning a new value to such a variable within the body of a method will not differ for the calling method).



It makes sure that any code in the method body that uses the argument value will use the original value passed by the caller and not some other value that was assigned to it inside the method body.

0


source







All Articles