This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regular Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regular Inner Class" Watch "Regular Inner Class" New topic
Author

Regular Inner Class

dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
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.


SCJP 1.4<br />SCWCD 1.4
Mukhtar Ahmed
Greenhorn

Joined: Nov 20, 2003
Posts: 9
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

Joined: Sep 04, 2003
Posts: 43
I think this is one of the reasons why you use inner classes.


Ana<p>SCJP 1.4
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
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
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Mar 07, 2003
Posts: 330
Got it. Thanks for the explanation Kathy!
Btw, great book!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regular Inner Class
 
Similar Threads
Inner Classes
Accessibility of private methods in inner classes
What is the access modifier on a regular inner class?
Confused on private modifier for classes
Static Nested Classes