wood burning stoves 2.0*
The moose likes Java in General and the fly likes What is advantage of declaring 'final' in the input parameter of a funcion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is advantage of declaring Watch "What is advantage of declaring New topic
Author

What is advantage of declaring 'final' in the input parameter of a funcion

ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
Hi,

What is advantage of declaring 'final' in the input parameter of a function?

public void myFunction(final Sting str);

The only thing I can't do is this inside the function:
str = new String();

But even if i do that, the 'str' passed in by the caller won't change.

So I don't see what is the advantage of doing this.

However, in C++, if the input reference is 'const', I can only call the public const method of that class. I don't think Java behaves like this:
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29259
    
140

Originally posted by ying lam:
But even if i do that, the 'str' passed in by the caller won't change.

Right. Not everyone knows that though. By putting the final, you can make it more explicit that people won't accidentally set the parameter to another value.

I don't make the parameters final and instead rely on a static code analyzer to tell me if any parameters are written to. Then I refactor the confusing code.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I can think of two advantages out of my head.

First, you *need* to declare it final if you want to reference it in an anonymous inner class.

Second, when working with legacy code, we often have to deal with very long methods (consider yourself lucky if you don't have to deal with such code). In such cases, declaring a local variable final can be of great help to understand the code.

For example, imagine that myFunction is 1500 lines long, and in line 1499 which reads

subStr = str.subString(2);

you occasionally get a NullPointerException. You think that str actually never should be null. With str not being final, you will have to look through the whole function to see whether it is assigned null somewhere. If you can declare it final without getting a compile time error, you instantly know that the problem isn't in the function itself - it the null value has already been passed to the function.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
Thanks.
You menion 'First, you *need* to declare it final if you want to reference it in an anonymous inner class.'

Can you please tell me why it has such restriction?

Normally, if I do this
public void aFunction(String str);
this won't change the str passing in.

So even for the anonymous inner class, the function won't affect the class passing in too, Right?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That's well explained at http://www.coderanch.com/t/372193/java/java/Anonymous-Inner-Class-final-objects
 
 
subject: What is advantage of declaring 'final' in the input parameter of a funcion
 
Similar Threads
Overloading constructors with differing intializations
const references
synchronization and _final_ modifier
VB & Java "Option" Question.
declaring local variables final?