My style is to not use final for local variables (including parameters and
catch variables) at all. Unless they're being closed over by an anonymous
class, where it's required. I think the point of a final qualifier is to
reassure the programmer (reading or writing the code) that it won't change
unexpectedly - it's an aid to reasoning about the code. Instance variables
can be read and written from anywhere in the class (or even outside, if
they're not private), and thus in a class of more than trivial size, it
can be hard to keep track of whether that's happening, and where.
Declaring a field final eliminates this difficulty at a stroke. However,
local variables can only be accessed from inside the method they're local
to; that's a much smaller body of code that the programmer has to keep in
mind to know everything about the variable's use. There, i don't think
there's much uncertainty to be reduced, and the additional keywords just
clutter up the screen and mean more letters to type.
Note that for this analysis to hold, your methods have to be short: small
enough that you can hold the whole thing in your mind at once. However, i
would say that this is exactly how methods should be. If your method is
big enough that making its locals final is helping you, then you need to
split it up into multiple smaller methods. Essentially, final locals are a
code smell.
I quite like this idea too. I'd want to see how it felt in practice before
i fully supported it, though.
for (nonfinal int i = 0; i < 10; i++)
Yup, a *big* improvement.
Even better, IMHO:
for (variable int i = 0; i < 10; i++)
!
tom