• 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

Is it a good programming practice to use final keyword in method parameter ?

 
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very confused to use final keyword in method parameter. When to use final keyword and when to not ? Does it really good practice to use final keyword in every method parameter ?

Thank you.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a matter of opinion. Some people think this is a good idea. I think it adds too much noise to the code, which makes it slightly harder to read.

One common idea is that you shouldn't change the values of argument variables inside a method, because it might be confusing - less experienced programmers reading the code might think that it changes the variable in the place where the method was called from, which is not the case. Making argument variables 'final' makes it not possible to modify argument variables (although, if such a variable refers to a mutable object, you could still modify the object that the variable refers to - and such a change would be visible in other parts of the program).

If you use an argument variable in an anonymous inner class inside the method (and you're using Java 7 or older), then you are required to make it final, because anonymous inner classes can only access final variables that are defined outside of the anonymous inner class (this restriction was removed in Java 8).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muztaba Hasanat wrote:I am very confused to use final keyword in method parameter. When to use final keyword and when to not ? Does it really good practice to use final keyword in every method parameter ?


My rule of thumb: Use final every place you can unless you know it's wrong. ie:
  • Class declarations - unless you know you're going to subclass.
  • Method signatures - unless you know you're going to override.
  • Member fields - unless you know you're going to need to change them.
  • Why? Because you can always remove it later if you need to, but you can't add it.

    That said, I don't often use it on parameters because I find it a bit "noisy" - but it's simply my personal taste. Off the top of my head I can't think of a single rational reason why not to, nor a single case where it would be incorrect to make a parameter final - although there are a few where you must.

    So basically, I'm inconsistent. Sue me.

    Winston
     
    Muztaba Hasanat
    Ranch Hand
    Posts: 50
    1
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


  • Class declarations - unless you know you're going to subclass.
  • Method signatures - unless you know you're going to override.


  • Thanks for the reply.

    I don't understand these two reasons. Are those like this ? -

     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Muztaba Hasanat wrote:I don't understand these two reasons. Are those like this ?]


    Yup.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    allen robin wrote:Hey,First of all,Java always makes a copy of parameters before sending them to methods means the final doesn't mean any difference for the calling code...


    That's not quite true. It may not make any difference grammatically but, as Jesper pointed out, this only happened in version 8.

    And it may still be the case that the compiler can make optimizations when a parameter is declared final that it can't otherwise (that used to be one of the reasons given for doing it habitually)

    Winston
     
    Ranch Hand
    Posts: 47
    Netbeans IDE Eclipse IDE Debian
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    allen robin wrote:Hey,First of all,Java always makes a copy of parameters before sending them to methods means the final doesn't mean any difference for the calling code i.e.,


    Not true, check your understanding how objects and primitives are processed awhen used as parameters.

    allen robin wrote:
    inside the method the variables can not be reassigned.On the other hand, if you reassign any new value within the method the compiler complains immediately.


    Also not true,
    Is valid code

    allen robin wrote:This means that there no complexity in this method.
    So,finally its good to use as it makes the code error free and easy.


    Code is not as error free as you might think. See this example:
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic