This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
Joined: Sep 05, 2003
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?
author and iconoclast
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.
Joined: Sep 21, 2003
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 ]
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: