Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Local variable scope

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i compile the following



The compiler complains with the following error



I thought the x in the for loop is in a different scope (block scope) than that in the main method.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's in a different scope, but it's still an overlapping scope because the outer x is still valid inside the brackets. And you aren't allowed to have two local variables clashing like that.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:It's in a different scope, but it's still an overlapping scope because the outer x is still valid inside the brackets. And you aren't allowed to have two local variables clashing like that.



ah ok i get it. Thanks.

Thinking about this.. is it not the same thing as declaring the same variable as an instance variable and another one in an instance block. i.e.



The instance block will use the x declared inside the block. I though the for loop was supposed to work the same way.


[/code]
 
reply
    Bookmark Topic Watch Topic
  • New Topic