• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FinalLocalVariable.

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic