File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Reg.naming variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reg.naming variables" Watch "Reg.naming variables" New topic

Reg.naming variables

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
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

Joined: May 10, 2001
Posts: 3244
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

Joined: Jul 13, 2001
Posts: 94
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"

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Reg.naming variables
jQuery in Action, 3rd edition