Use final
October 27, 2009 by Frank Salinas · 2 Comments
The final keyword is one that I find is under utilized. Use final in your projects as a means of defensive programming. If you are unfamiliar with this keyword here is a great article on its usage.
Renaud Waldura’s The Final Word On the final Keyword .
No related posts.
Its so under utilised, its a shame that final wasn’t the default and a keyword like ‘var’ wasn’t used for mutable variables/fields.
I suggest you make all fields final if possible. While performance is a consideration, I believe it makes it much clearer which fields can be changed over the life of an object.
@Peter – I agree. If developers could just forget about the potential performance optimizations final may, or may not, provide and focus on the intended purpose of the keyword for preserving immutability code quality would increase. In my opinion, code quality comes before performance and leveraging defensive programming techniques such as enforcing immutabilty appropriately increases code quality.