• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion over method parameter and instance variable having same name  RSS feed

 
chinny mn
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!