• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Inner Class

 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
Why can multiple regular inner classes access each other's private members (variables and methods) via their instance variables? What's the purpose of them being private?
Thanks.
 
Mukhtar Ahmed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my first post on this great site. So please bear with my ignorance but what is a "regular inner class"? Can you provide an example?
 
Ana Abrantes
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is one of the reasons why you use inner classes.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I'm actually trying to say is that the private access modifier doesnt seem to work methods defined inside regular inner classes. I understand that there is a special relationship between the outer class and the regular inner class. The inner class can access the outer class' members (variables/methods) even those declared private. The outer class, likewise, can access the regular inner class' methods including the private ones. But what about multiple regular inner classes (within an enclosing class)? Why can they access each other's private classes/variables? Shouldnt they be private to other classes except for the enclosing class for which they have a special relationship?
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- it helps me if I think of inner classes as "members" of the enclosing class. Even though they're classes, they are still a part of the enclosing class. And remember, when something in a class, such as a method, is marked private, that means private to the *class*, not private to the *instance*. There is no access restriction that means *private to the instance*. Any instance can access the private members of a *different* instance of the same type.
So inner classes are just a broader application of that *private to the class but not to the instance* behavior. And if you think of inner classes as being a helper part of the enclosing class, then it makes more sense that different inner class members of the same enclosing class would want that same special access to one another's private stuff.
cheers,
Kathy
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks for the explanation Kathy!
Btw, great book!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic