• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

scope of local variables in java

 
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic