• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's well explained at https://coderanch.com/t/372193/java/java/Anonymous-Inner-Class-final-objects
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic