This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes FinalLocalVariable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "FinalLocalVariable." Watch "FinalLocalVariable." New topic
Author

FinalLocalVariable.

Muthaiah Ramanathan
Ranch Hand

Joined: May 16, 2005
Posts: 102
I was using Checkstyle to check the style and coding conventions. It spit out all the parameters that are not declared as final and was complaining that it better be as the object or data type is not changed.

I was wondering if this is a better practice, as for me atleast, it is all over the place. Or should I just leave it as it is?


SCJP 1.6, SCJD, SCWCD, SCBCD.

Be nice to people on the way up cos, you'll need 'em on your way down - From somewhere I can't remember!
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I always declare reference variables as final unless, of course, a different object will be referred to. I feel that my code is much easier to read.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Frans Janssen
Ranch Hand

Joined: Dec 29, 2004
Posts: 357
Hi Muthaiah,

I am not familiar with Checkstyle, but apparantly this is a tool that suggests that all method parameters which are not modified in the method should be declared as final?

I certainly would not appreciate it if my coworkers would declare every variable thatcould be final as final*. It's a matter of style of course, but I cannot recall any code examples where this was done extensively, so I guess it isn't a general practice.

Therefore I would advise not to follow Checkstyle's suggestions in this case. You could argue (if you wanted to) that it would be confusing for the junior programmers that Sun is referring to in the instructions.

Frans.

* I estimate that 99% of all method parameters is never modified. So if we followed Checkstyle's suggestions, that would mean that all but a few parameters would have to be declared as final! Not a readibility improvement as far as I am concerned!


SCJP 1.4, SCJD
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi,


I am not familiar with Checkstyle also but I am use a lot of final in my code.
A do this to keep the code easy to read/uderstand/maintain my code.
Each variable musts have a single scope and most of the times it caries a single value - there are also exceptions from this roule (by examble on iterations).


Regards

Mihai.


SCJP, SCJD, SCWCD, OCPJBCD
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I estimate that 99% of all method parameters is never modified. So if we followed Checkstyle's suggestions, that would mean that all but a few parameters would have to be declared as final! Not a readibility improvement as far as I am concerned!

I second that. I happily use "private static final" and "public static final" for class level constants (and sometimes to make the methods and classes final), but to inject that to every local reference declaration or a method parameter is abuse. Just for curiosity, I ran a search against all java.* classes in JDK1.4 (that's about 1200 classes) and found very few lines of code where final was used as a qualifier with a local variable or method parameter.

I would also note that "Java Coding Conventions" only prescribe the use of final with the class constants.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: FinalLocalVariable.
 
Similar Threads
ait and notify again
Queries
Appearing for SCJP in 3 days
clarification on final methods
JSP's compiled servlets