• Post Reply Bookmark Topic Watch Topic
  • New Topic

Should i defined all the local variables and method parameters as final?  RSS feed

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using findbugs and PMD as a code analyser. I am keep getting warnings to use local variables and method parameters as final.

I want to know if its a good practice to keep them final or what to do?

Sometime i have a private method which is 1 line longer. Sometime it is annoying to use final.

What you guys suggested?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, prefer local variables to class variables and make parameters final as much as possible.

Local variables reduces the scope of the variable, which is a good thing. It reduces the possibility of variable interference. Final parameters helps the compiler optimize.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my IDE configured to automatically make variables and parameters final whenever it can. I'd definitely say its good practice to do this.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have mixed feelings about using final for parameters and local variables. If you look around enough, you can find many arguments for and against its use with these. I seem to remember one presenter at a No Fluff Just Stuff symposium I attended a number of years ago, I believe it was Ted Neward, saying that some of these recommended uses of final don't really give the touted benefits.

I tend to leave final out just because there are so many different takes on whether or not it really is useful and because it just raises questions that are difficult to answer definitively. In particular, when people say that it helps improve performance, I tend to leave it out instead. Also, if you use final too liberally then it's harder to tell if final was just used out of habit or if it was a conscious and deliberate design choice.

Pretty much the only time I use final are the usual, more widely understood cases: with variables that need to be accessed by inner classes, to make methods non-overridable by subclasses, and to prevent a class from being extended.

Here's a sampling of articles, if you're not already confused about final:
http://stackoverflow.com/questions/4279420/does-use-of-final-keyword-in-java-improve-the-performance
http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html
http://www.ibm.com/developerworks/java/library/j-jtp04223/index.html
http://www.javaperformancetuning.com/tips/final.shtml - no mention of final parameters
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment I'm working in a team where we make all local variables final, unless there's a reason not to. For some reason we don't do this with argument variables.

I agree with Ted Neward, who Junilu mentioned, that in practice it doesn't really give a lot of benefit. I can't remember ever having a bug that could have been prevented by making the variable final.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is so much confusion. I have read several articles mentioned by the Junilu and self searched on the google, no where it is mentioned that it
has any impact on the performance or much benefit but still some people suggesting or making every local variable and method argument as final.

So i thought it is like personal choice to follow this or not.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:So i thought it is like personal choice to follow this or not.

Make the choice as a team if you are programming in a team environment. The other con in using final too liberally is that rather than clarify code, it tends to make it more verbose. It's not too bad when developers have the discipline to keep methods short and sweet but nine out of ten times this is not the case.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also... in some cases, does it really matter? Do you really need to declare the parameter to a one line setter method as final? Or even need to add a comment to the one line setter method that it is a setter method? ...

I guess that it doesn't hurt, but I don't see much (if any) benefit either.

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: Final parameters helps the compiler optimize.

Is this still true? With Java 8, we have the "effectively final" concept. Which treats variables that could be final as final even if they keyword isn't there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!