• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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: 23811
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: 23811
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!