This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
oli mueller
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...
 
Oladeji Oluwasayo
Ranch Hand
Posts: 101
Java Netbeans IDE Oracle
  • 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.
 
Roel De Nijs
Sheriff
Posts: 9926
111
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic