This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reg.naming variables  RSS feed

 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it ok if an instance variable & local variable share the same name in a program?
for ex:

[ April 12, 2002: Message edited by: Cindy Glass ]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syntactically and according to the JLS sure it is ok, but anyone that comes behind you might to maintain the code not like it.
 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer.Ya,what u say is right.It's difficult to maintain.
i just wanted to know if it was syntactically right.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The trick is - if you have a local variable shadowing a member variable, and you actually want to to something to the member variable, then you need to refer to it as "this.b" to distinquish it from local variable b.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!