• 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

Method variable should always be marked 'final'

 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you guys say?

If you need to do any operation on it create local variable and assign the parameter to local variable.

For example,



 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems pretty useless. A method parameter is equivalent to a local variable. If you want to change it, just change it.

I know that you can have at least NetBeans warn about assigning values to a method parameter. Supposedly because it may lead to confusing code. I don't really buy that myself.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Seems pretty useless. A method parameter is equivalent to a local variable. If you want to change it, just change it.

I know that you can have at least NetBeans warn about assigning values to a method parameter. Supposedly because it may lead to confusing code. I don't really buy that myself.



I think that it has something to do with coding conventions.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only “need” to mark local variables or parameters final when they are used inside an anonymous or local class.
It is probably a good code convention to mark them final to remind yourself they are not changed, but, Stephan is right; you can change local variables if you wish. Changing parameters might cause confusion, but that does not merit a rule.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic