Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

scope of local variables in java  RSS feed

 
Ranch Hand
Posts: 46
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a confusion in below piece of code :



On line no 4. "duplicate local variables" compilation error comes.

please help me understand why?

Because as per my knowledge, the ouch variable in for loop has it's scope limited to for loop. can it not supersede the method parameter

ouch

here? The way method parameters will supersede member variables?

Thanks.
Dish
 
author
Posts: 23816
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The ranch office forum is for questions / discussions regarding the coderanch site. Let's move this topic to the Java in general forum.

Henry
 
Henry Wong
author
Posts: 23816
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dishi Jain wrote:


On line no 4. "duplicate local variables" compilation error comes.

please help me understand why?

Because as per my knowledge, the ouch variable in for loop has it's scope limited to for loop. can it not supersede the method parameter

ouch

here? The way method parameters will supersede member variables?



See section 6.4 of the Java Language Specification (http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#d5e6988), shadowing of a local variable is not allowed in Java.

Henry
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!