It's not a secret anymore!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Your opinion on the usage of final {variable, params, properties} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Your opinion on the usage of final {variable, params, properties}" Watch "Your opinion on the usage of final {variable, params, properties}" New topic
Author

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

oli mueller
Ranch Hand

Joined: Feb 13, 2011
Posts: 42
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: 101

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: 5131
    
  12

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: 2258
    
    3

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Your opinion on the usage of final {variable, params, properties}
 
Similar Threads
CheckStyle Rule : Making final to the local variables
Constants (need your votes on best declaration)
Auto-boxing/Auto-Unboxing
Final modifier and Java performance tuning
SCJD - Code Conventions using logs and assertions