Java Code Static Final variable usage-Collection of common programming errors

Setting the reference to final ensures you can’t change the reference. Note however that if the object referred to is mutable then you could still change that (not in this instance, since Strings are immutable).

I normally make fields final and initialise them in the constructor. By favouring immutability my classes are easier to debug and are more likely to be safe in threaded environments. It’s easier to remove the immutability constraint than add it.

I also do this for method arguments. Rarely (ever) do I want to change a method argument, and making them final will catch inadvertent assignments.

I try not to use static except for final constants. Unless it’s something like a logger, I don’t really want one instance per class, and (of course) this doesn’t work in the case of multiple classloaders. It’s also getting close to the singleton anti-pattern, and this impacts on easy testing and (potentially) threading.