aspose file tools*
The moose likes Java in General and the fly likes final variable as parameter to method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "final variable as parameter to method." Watch "final variable as parameter to method." New topic
Author

final variable as parameter to method.

Babu Kopparam
Greenhorn

Joined: Dec 22, 2003
Posts: 1
Can some tell me the significance of mentioning a parameter to a method as final.
For ex.
public String getAddress(final String userid) {
........
}
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
Can some tell me the significance of mentioning a parameter to a method as final.
For ex.
public String getAddress(final String userid) {
........
}

final arguments are important in such a way that whatever value it has when it was passed CANNOT be modified within the method that was invoked.
Example:
public static void someMethod{
String userid = "Mark Nelson";
String address = getAddress();
System.out.println("Name: " + userid);
System.out.println("Address: " + address);
//Mark Nelson, an avid customer, has now been changed to John Wayne!

}
public static String getAddress(final String userid){
userid = "John Wayne";//OOPS! Compiler error!
return "Austin, Texas";//How come Austin Texas is returned for a
//nonexisting user id?
}


Thinking is the talking of the soul with itself...-Plato
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
I spotted a syntax error in my example. Pardon me for that. Supply the parameter in static getAddress:
String address = getAddress(userid);
Thanks!
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Originally posted by Babu Kopparam:
Can some tell me the significance of mentioning a parameter to a method as final.
For ex.
public String getAddress(final String userid) {
........
}

This is a bad example to illustrate the use of the keyword [/i]final[/i] in Java. Note that Strings are immutable, i.e. once you assign a value to a String, there is no way to change it. No matter what you would do in the method above, the value of userid will be exactly the same after the method invocation as it was before the invocation, and it's irrelevant whether you declared the parameter it final or not.
A better example would be to replace the String parameter with the user-defined type. For example:

Note also that final doesn't prevent you from changing the state of the object, -- if it has setters, you can mutate the object in the method body and it will be mutated when you return. The only thing that compiler enforces is that the reference to the object is not modified.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Hi All,
Seeing this topic I would like to share my thoughts about use of keyword 'final' with method-arguments.
Eugene geave an important point about Strings being immutable. However, even with other classes, such use of keyword 'final' does not serve the actual purpose.
With any class (including immutable classes, such as String and wrapper classes), if the parameter isn't declared 'final', one can only force the reference to point to another object (or 'null'), but cannot change the actual reference (in the caller method) to point somewhere else. Primitives are anyway transferred by VALUE, so changing them inside any method also will not change the actual arguments.
Let's just say, EVERY VARIABLE/HANDLE (Object References as well as Primitives) in Java is passed by VALUE. Hence, there is never a danger of 'loosing' or 'distracting' a handle by passing it to any method.
Only benefit you get out of such a use of keyword 'final' is that you can catch errorneous assignments/initializations of parameters at compile time, which ought not to be there. For example, if a method is taking parameters i, j & k, and your design ensures that the method MUST not change any of these, then by declaring the parameters 'final' you can catch any errorneous re-initialization of these variables (arguments) inside the method at compile-time only, which ofcourse could prove to be potential headahe-saver.
Regards,
Aditya
Ganesh mb
Greenhorn

Joined: Dec 30, 2003
Posts: 3
As Aditya rightly mentioned, any re-initialization of the passed variable is not going to have any effect after the method call returns and having the variable name as final serves the purpose of warning the user during compile time.
Here is another reason I can think of for passing final parameters. Compiler expects variables to be final for some operations, For instance, switch-cases expect final variables and if the passed parameter needs to be used here, they ought to be final. And, if you have some method-local inner classes these final parameters can be used in that method.
Thanks,
Ganesh
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Declaring method parameters as final is a suggestion made by Joshua Bloch, "Effective Java Programming", which, in my opinion, is one of the most invaluable resources for Intermediate/Advanced Java application development.
This suggestion is made simply because there is never (I say this loosely as I cannot foresee every possible problem) a good reason to modify a parameter method. Some might argue that reuse of the declaration improves performance (negligibly) and perhaps even improve readbility. Personally, I disagree with both of these reasons, although, I do not declare my method parameters final, simply because I trust myself not to abuse the language as such.
Given my opinion, and the fact that I tend to consider the opinions of other developers of similar experience, I will also point out that some of these developers disagree with the suggestion made by Bloch. Be it also known, that some developers create their opinion in a misinformed manner, since they don't understand the pros and cons of such a practice.
At the end of the day, it's all up to you to decide, since it is only your method implementations that will be affected.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variable as parameter to method.