aspose file tools*
The moose likes Java in General and the fly likes final keyword for method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "final keyword for method parameters" Watch "final keyword for method parameters" New topic
Author

final keyword for method parameters

Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
Hi,
What is the significance "final" for a method parameter?
void foo(final int p) {
}
Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It's the same as for any other final variable: it means the value of the variable can't be changed.


[Jess in Action][AskingGoodQuestions]
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
Hi Victor,
This is a very popular way of writing a method. The idea is that the there is normally no reason why you would want to change the value in the parameter. I have seen a number of bug where people have changed the value of the local var instead of the object var. For example

Ok this example looks simple to spot, however when this happens in a more complex method, it is not so easy.
To stop this type of mistake a lot of people make the parameter final:

That way the above code would not compile. If this bug still gets to your customer then, you have a big problem. Why has the customer got code that does not compile?
Hope that helps
Chris
Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
Thanks for the reply.
Hm...now that I understand what is the usage, it doesn't look to me that useful though. Parameter passing in Java is all pass-by-value, if one changes the value of a parameter variable, the effect goes away after the method finishes execution. So, I would say assigning a value to a parameter variable is never useful in Java (except maybe you want to avoid creating an extra temp. variable in the stack).
Comparing to C++, const parameter is much more significant, since it could be pass-by-reference.
What do you guys think?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It's useful as a documentation techinque. If you know the value of a parameter won't change, then it's easier to understand the method. Also, local variables referred to by anonymous and method-local classes have to be final, so to use a method parameter in one of these, it must be final.
Note that there's actually an RFE in Sun's Developer Connection Bug Database for adding C++-like "const" to Java; it's got something like the third or fourth largest number of votes.
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
I tend to make all may parameters final. It may be easier if my example was a little more complex:

Hmm, that may be a bug.. value = value - value;.

I have see people put _ at the start of a parameter var e.g:


In may my option the _ does nothing but make you code more complex. All you need is another developer who does it the nother way around, i.e put _ at of object vars in the same project. It can all get a little complex when there is no need to be.
Chris.
[ March 05, 2004: Message edited by: Chris Harris ]
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Augh, Mr. Harris...
I avoid the class-level vs automatic name conflict in the first place:

No ambiguity. Having said that, I too am a big proponent of using final for automatic variables, whether arguments or not, in method bodies. Though, I'd like to point out one thing Victor Ho said:
Parameter passing in Java is all pass-by-value...

This isn't true. All primitive parameters are passed-by-value. All object parameters are passed by reference. In the case of passing an object as in my example setBar() above, the final modifier would simply prevent b from being reassigned to point to another object. It does not prevent me from writing code in the setBar() method that modifies the internals of the object. For example, this will execute:

This will not:

sev
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
The final modifier has a special meaning with inner classes. An inner class created inside a method can only use a local variable if it is declared final.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final keyword for method parameters