• 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

Confusion over method parameter and instance variable having same name

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now Does the method modifyName behaves like a setter and changes the instance variable name to be "Dave" or modifyName cannot behave like setter but treats name as only the method parameter.

Does the method modifyName cannot be a setter only because of the naming convetion? modifyName() treats name as method parameter just because of not using JavaBean naming conventions?

if we change the name of the method modifyName() to setName(), then it changes the instance variable name?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're overcomplicating things. In this line

name and name are the same variable (the method parameter), so you are setting name to its current value.
To get round it you either use

which apparently you don't want to (why not ?)
or you change the name of the method parameter

The name of the method in all this is irrelevant.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chinny mn wrote:// I know using 'this' would be helpful, but I dont want to


This is completely mystifying. What's your reluctance to using this?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chinny mn wrote:


There are some places where using this is a matter of style. Some people prefer to be explicit everywhere, others don't. Perhaps by this you mean that you don't.

But there are some places where the use of this changes the meaning of the statement entirely. Here, name and this.name are entirely different variables.
 
Create symphonies in seed and soil. For this 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