This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variable shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variable shadowing" Watch "Variable shadowing" New topic

Variable shadowing

Kaushal Shah

Joined: Jul 20, 2004
Posts: 4
"A variable declared within the for-loop can not shadow a local variable of the enclosing method."

Could anyone give me an exaplanation and an example for this one?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

results in two errors: a is already defined in aMethod() and b is already defined in aMethod().
It's explained in Section 14.4.2 of the JLS
[ July 30, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
It is sorta covered in the JavaRanch Style Guide.
subject: Variable shadowing
It's not a secret anymore!