• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final keyword for method parameters

 
Victor Ho
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the significance "final" for a method parameter?
void foo(final int p) {
}
Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the same as for any other final variable: it means the value of the variable can't be changed.
 
Chris Harris
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 268
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic