Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes Method variable should always be marked 'final' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method variable should always be marked Watch "Method variable should always be marked New topic
Author

Method variable should always be marked 'final'

Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 512
What do you guys say?

If you need to do any operation on it create local variable and assign the parameter to local variable.

For example,



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3679
    
  17

Seems pretty useless. A method parameter is equivalent to a local variable. If you want to change it, just change it.

I know that you can have at least NetBeans warn about assigning values to a method parameter. Supposedly because it may lead to confusing code. I don't really buy that myself.
Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
Stephan van Hulst wrote:Seems pretty useless. A method parameter is equivalent to a local variable. If you want to change it, just change it.

I know that you can have at least NetBeans warn about assigning values to a method parameter. Supposedly because it may lead to confusing code. I don't really buy that myself.


I think that it has something to do with coding conventions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41616
    
  31
You only “need” to mark local variables or parameters final when they are used inside an anonymous or local class.
It is probably a good code convention to mark them final to remind yourself they are not changed, but, Stephan is right; you can change local variables if you wish. Changing parameters might cause confusion, but that does not merit a rule.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method variable should always be marked 'final'