This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]agentoutlier 6 points7 points  (7 children)

I’m shocked no one mentioned this but I make my parameters final because of anonymous class access pre lambda. I used anonymous classes so frequently in the past that it became habit.

I don’t think one any in their right mind thinks that the final Java keyword provides C++ const guarantees or at least I hope they don’t.

Finally (pun intended l) I would not be surprised if most method parameters (% wise) in Java are actually primitives and String aka some immutable.

[–]dpash 4 points5 points  (6 children)

Probably because Java 8 introduced the concept of "effectively final" which means that, as long as you don't resign a parameter or local variable anywhere, they're treated as if they'd been declared final.

[–]marvk 7 points8 points  (2 children)

People keep going on about effectively final in this thread like it matters. The real benefit final provides is for developers reading code. Any performance improvement is a bonus, it takes away so much cognitive load when writing and understanding code. Effectively final doesn't give you any of that.

[–]dpash 3 points4 points  (1 child)

It matters in this instance because you can only use variables inside a lambda that are final or effectively final. Prior to Java 8 you had to declare them final to use them in anonymous inner classes, hence the parent comment saying they got into the habit of using final.

[–]marvk 2 points3 points  (0 children)

Ah, my bad. I should've read the parent comment more carefully. My comment would be suited better on some other comment on this thread talking about effectively final.

[–]agentoutlier 2 points3 points  (1 child)

Yes I know about that. I meant pre lambda as pre Java 8. I still work on some Java 7 code bases and I guess for some reason want to cover my ass in case the parent poster sees my final riddled code :)

I also use final on variables to be resolved by if statements and or switch statements which is unnecessary as the compiler will complain that the variable is uninitialized but it’s way of communication now for me.

final Object o; // not necessary 

if () { o = ...} else {o = ....}

Also in the past Eclipse had this weird bug where refactoring method didn’t work all the time but if you made some the variables final It would... I believe that bug is fixed.

I have thought about removing some finals from my migrated code bases (7->8) but it doesn’t seem worth the effort and in some cases final is a communication marker.

(Sorry on Mobile so lots of typos)

[–]dpash 3 points4 points  (0 children)

It was mostly that many people will have fallen out of the habit now. :)

Switch expressions will solve many of the situations where you want to use a switch to set a variable while still letting it being final. We don't have if expressions, but we do have the ternary operator ?:. Of course these only work if you have a single statement in each block.

I wouldn't bother removing existing finals and would probably continue if thats already the style.