Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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
Sheriff
Posts: 23484
138
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
Sheriff
Posts: 23484
138
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!