views:

148

answers:

4
A: 

Parameters which are not marked as final can be changed, in the same manner as local variables. This is usually not desireable, but the language permits it. Marking the parameter as final prevents this.

RMorrisey
+6  A: 

Using final :

  • clearly communicates your intent
  • allows the compiler and virtual machine to perform minor optimizations
  • clearly flags items which are simpler in behaviour - final says, "If you are looking for complexity, you won't find it here."

From http://www.javapractices.com/topic/TopicAction.do?Id=23

DJ Quimby
+1: I agree with most of your points. However, "minor optimizations" may even be an overstatement for modern virtual machines as some (including HotSpot, I believe) automatically *assume* the arguments to be final, until proven otherwise.
Adam Paynter
@DJ Quimby, right, and as it says in the doc you link to, "Others find this verbose, and of little real benefit." That's where I wanted to START the question. I already know what `final` for parameters means.
Yar
@Yar, Yes, agreed, and I also agree with you and Adam that the minor optimizations are likely not worthwhile. Still, the final keyword does in fact do the three things above. I would say that beyond these points it would really be a matter of personal preference. For the sake of brevity and readability, I am with you.
DJ Quimby
+1 for agreeing with me ;) Seriously, though, Java is verbose enough without adding even more. I see the point of `final` classes, methods, static and instance vars. But method params? My new question is: how does final affect a client class using the method?
Yar
The optimization aspect of this is irrelevant for the vast majority of cases. The intent aspect is relevant sometimes, but readability and conciseness should always be weighed against it.
Peter DeWeese
+1  A: 

The answers to the SO question http://stackoverflow.com/questions/154314/when-to-use-final explain why it is a good idea to declare parameters final. Clearly, the people who created the PMD default ruleset agree with this.

However, use of final is not mandatory in this situation, and not required by some Java style guides. If you think that final makes your code untidy and verbose, you are free to tailor the PMD ruleset to exclude this rule.

To be honest, I don't think there's anything much to discuss here. Besides, SO is not a suitable forum for "discussion" and airing one's grievances about tools, etc. Try http://programmers.stackexchange.com

Stephen C
Thanks `Stephen C`, I wasn't trying to stir up discussion: I thought that somebody could perhaps give me some insight as to why not using `final` makes my code horribly ambiguous. On the PMD front, the rule to delete (or whatever) is called `MethodArgumentCouldBeFinal`. The best answer in the link you provide is great and basically makes the point I was wondering about: final for method params is perhaps overdone.
Yar
Also: I'm NOT airing any grievance about PMD: the tool is completely flexible, great, and allows for changing the rules as one sees fit. I don't think the PMD designers agree with marking methods params as final, or it would be all over their source code, which it's not.
Yar
+5  A: 

oh really, let's look at PMD's own source code:

http://pmd.sourceforge.net/xref/net/sourceforge/pmd/ant/PMDTask.html

among 20 or so method parameters, ZERO parameters are final.

irreputable
brilliant and hilarious. +1
Yar