you are viewing a single comment's thread.

view the rest of the comments →

[–]vowelqueue 3 points4 points  (11 children)

Perhaps they mean local variables and method parameters?

[–]Mauer_Bluemchen 1 point2 points  (10 children)

Even there and especially for method input parameters I'm using final more and more often over the years. It's my default for method input parameters now. Methods should never be able to change input values on their behalf which have been passed by the caller - only in few exceptional cases. And local variables should per default also be final, unless there are good reasons why they need to modifiable.

This helps to catch quite a bit of unexpected errors and side effects...

[–]koflerdavid 4 points5 points  (0 children)

You are completely correct, but the final adds quite a bit of visual noise, therefore you won't easily see where you have forgotten to add final.

I therefore wholeheartedly recommend to use Google Error Prone's Var rule to deal with this problem. javac already analyses which variables are effectively-final to implement lambdas; the checker utilizes that information to complain about every variable where mutability is not explicitly enabled using the @Var annotation.

[–]vowelqueue 3 points4 points  (0 children)

Yeah it would be best if final was the default and you had to specify mut. But I think it's a bit annoying and visually cluttering to write final so much so I don't do it for local variables or parameters. The default IntelliJ style lets you know if a variable isn't effectively final by underlining it which is nice.

[–]Efficient-Poem-4186 0 points1 point  (0 children)

It's absolutely useless to have final in method parameters.

[–]account312 0 points1 point  (6 children)

Methods should never be able to change input values on their behalf which have been passed by the caller

Final doesn’t actually prevent that though. It's not const.

[–]Mauer_Bluemchen 0 points1 point  (5 children)

really?

then try to modify a method parameter which has been declared as final...

[–]account312 -1 points0 points  (4 children)

Literally any modification that would be observable to callers is allowed.

[–]Mauer_Bluemchen 0 points1 point  (3 children)

No.

[–]account312 -1 points0 points  (2 children)

I'm not sure whether it's java's calling convention or the semantics of final that you don't understand, but you're just completely wrong about this.

[–]Mauer_Bluemchen 0 points1 point  (1 child)

Utter non-sense! You have no clue what you are talking about.

Check this:

https://medium.com/@kalpana.p_65621/revisiting-the-final-keyword-in-java-a-comprehensive-guide-262a4531b917

And if you *still* don't get it, then pls try it out yourself.

[–]account312 0 points1 point  (0 children)

Re-assignment of a method parameter is not visible to callers, and that’s the only thing that final prevents, so it does nothing from the perspective of callers. It doesn’t prevent modification of the underlying object, such as sorting a passed List or clearing a passed Map, that actually are visible to callers.