Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's well explained at http://www.coderanch.com/t/372193/java/java/Anonymous-Inner-Class-final-objects
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic