Why shouldn't all function arguments be declared final?

java, oop, pmd

Solution

I can think of only 2 reasons not to make a parameter `final`:

to save the use of a local variable if you need to overwrite the parameter's value in some edge cases (for instance to put a default if the param is null etc.). However, I wouldn't consider that a good practice in general.

to save 6 characters per parameter, which improves readability.

Reason 2 is what leads me not to write it most of the time. If you assume that people follow the practice of never assigning a new value to a parameter, you can consider all parameters as implicitly `final`. Of course, the compiler won't prevent you from assigning a parameter, but I can live with that, given the gain in readability.

Problem

Ok, so I understand why we should declare an argument to be final from this question, but I don't understand why we shouldn't... Since Java always uses pass by value, this means that we can't return a new value through the given argument, we can only overwrite it, and make the argument useless therefore, because we don't use the passed value... Is the only benefit of non-final method arguments in Java the fact that you don't have to make a local variable of the arguments' type? P.S. This question was triggered by `PMD`'s rule of `MethodArgumentCouldBeFinal`

Original source

Related problems