• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why are parameter types declared with final modifier?  RSS feed

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have come across some code where the method signature is like,



I fail to grasp why final is needed here. Is it only because of the field immutabilty? What big picture am I missing in this case?

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the value of a parameter during the execution of the method's algorithm can be confusing and should be avoided by marking the parameter as final.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of two reasons.
  • 1: To signal to users that the parameter is never reassigned.
  • 2: If a local variable or a parameter is used in a local class, it needs to be final so the compiler knows the local class' variable is the same as the original field/parameter.
  •  
    Ahsan Bagwan
    Ranch Hand
    Posts: 254
    1
    Java MySQL Database Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks to both of you.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    Randall Twede
    Ranch Hand
    Posts: 4696
    8
    Java Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    IMO final and static are kind of similar. use them any time it fits.
    i understood Campbels first reason and that is why i have started doing it. it helps me reading someones code if i know the parameter will not be changed. often we do pass parameters and change them.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7987
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually, quick and dirty approaches for the two are completely opposite. Use final as much as possible. Use static judiciously.

    I try to make most of my classes immutable. You can't do without final. Even in mutable classes, collection fields and most other fields are usually final. The class itself is almost always final. However, I tend not to make my method parameters final, because it clutters my method signature.

    Static on the other hand I try to avoid as much as possible. Preferably I only use the static modifier for constants, "pure functions" and for classes that are nested for namespace reasons, such as ChessPiece.Color.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote: . . . Use final as much as possible. Use static judiciously. . ..
    Agree.

    The two keywords mean totally different things. And remember that Java is pure pass‑by‑value, so reassignments of a parameter are not reflected in the original values.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!