| Author |
Your opinion on the usage of final {variable, params, properties}
|
oli mueller
Ranch Hand
Joined: Feb 13, 2011
Posts: 40
|
|
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
Joined: Sep 10, 2010
Posts: 99
|
|
|
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.
|
OCPJP 6, OCMJD 6
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4347
|
|
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.
|
SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2212
|
|
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.
|
Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
|
 |
 |
|
|
subject: Your opinion on the usage of final {variable, params, properties}
|
|
|