aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why private isnt private, story about Inner Class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why private isnt private, story about Inner Class." Watch "Why private isnt private, story about Inner Class." New topic
Author

Why private isnt private, story about Inner Class.

Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
Hi.
Lately I discovered that classes can access private members of its inner classes.
I thought private means private; only for me and my members.

Can anyone explain me, why is that possible ?


[ June 22, 2006: Message edited by: Lucius Stephienn ]

SCJP 5.0 (91%), SCBCD 1.3 (94%), SCWCD 1.4 (88%)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I looked in the Java specification, and I didn't find anything explicitly stated about this. My guess is that because an inner class is only defined with respect to an instance of it's enclosing class, the enclosing class and other inner classes can have access to its private members.

I didn't find a bug report about it. Also I downloaded Mustang and it compiled fine, so my guess is that it must be a feature and not a bug, although I haven't found any confirmation about that.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
From here:

The outer and inner classes can access each other's state, even if it is private. Stylistically, they are basically one implementation code base, so mixing access is ok, but we prefer receiver-relative coding for both the outer and inner classes where it makes sense.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Keith]: I looked in the Java specification, and I didn't find anything explicitly stated about this.

It's part of the fundamental definition of what private means in the first place. From JLS3 6.6.8:
A private class member or constructor is accessible only within the body of the top level class (�7.6) that encloses the declaration of the member or constructor.

The phrase "within the body of the top level class" is key. The word private has always allowed access within the same top-level class. This isn't as widely known as it probably should be - I'm guessing that's because people often learn about access modifiers before they learn about nested classes, and so they learn a simplified version of private that doesn't mention nested classes.
[ June 22, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Thanks Jim. I was looking in the wrong place. I went through the part that was specifically about inner classes.
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
Hi,

A private class member or constructor is accessible ONLY within the body of the top level class (�7.6) that encloses the declaration of the member or constructor.


Only, hmm, so why is it accessible ALSO within the body of the inner class?
Is the the above definition clear indeed?
I am not an english speaker.
[ June 22, 2006: Message edited by: Jasiek Motyka ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The inner class is part of the body of the top-level class.
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
Thanks , I have never seen it this way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why private isnt private, story about Inner Class.