Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Your opinion on the usage of final {variable, params, properties}

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

one thing that actually interests me: What do you think about the usage of finals??...I usually declare class members final whenever I can (see Bloch - favour immutables)...and I think there is not much discussion that this is a good approach....but in my company we have a tool that complains when I dont declare local variables and parameters as final when they could be declared final....so, recently I tend to declare EVERYTHING final whenever I can...

...what is your opinion about this and have you made use of it in your certification??...I mean there are many good things about declaring variables etc. final, but it also clutters the code....

THANKS for your opinion...
 
Ranch Hand
Posts: 101
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring local variables (passed as parameters or declared in the method) as final or not shouldn't raise eyebrows, since it won't be visible outside the method (scoping, OCPJP, remember?). Fields however depend on your design. You want a constant (e.g GRAVITATIONAL_CONSTANT) use final, any other thing, no final. Thats pretty simple or maybe I don't understand the question.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my assignment I declared every variable final when it was possible. It gives any other developer who looks at my code that the value of this variable (or parameter or data member) will never change (without having to scan the complete block of code or even class). So from that perspective I think it's a good idea to use it (and it could be done automatically for you by your IDE with some configuration).

In my working job we decided not to use the final keyword for local variables or parameters, unless it is required (e.g. when you want to access them from an inner class). For data members we do use them (when possible) for the reason discussed in the 1st paragraph.
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:In my assignment I declared every variable final when it was possible



Me too. On my daily work, I declare variables as final wherever possible.
 
Paper beats rock. Scissors beats tiny ad.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic