• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner classes and variable scope

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you initialize a private variable and refer to it in a class and in an inner class, are you referring to the same instance? Something like this :
class MainClass
{
private int i;
private void method()
{
i = 10;
}
class InnerClass
{
i = 5;
}
}
Am I referring to the same i in both cases? One thing that surprises me is that this compiles successfully because I thought you could only access a private variable from the same class. It appears you can access them from inner classes too???
Also :
If my inner class is a Thread will the change of i to 5 be 'seen' by the MainClass?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


hope that helps
Dave

keep in mind that this is for this type of inner class the other types (static, local, and annonymous) all behave slightly differently as far as access and visibility go.

[This message has been edited by Dave Vick (edited July 18, 2001).]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's really the reason for inner classes, isn't it... To provide a clean avenue for generating a relationship between two classes that provides access to private features... While Sun was revamping Java's event handling it became a major issue. So yes, an Inner Class can access the enclosing class's private features.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
You REALLY need to read this - it is good for a laugh if nothing else:
http://www.javaranch.com/campfire/StoryInner.jsp
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic