I have a small query regarding inner classes and access to private fields.
I would like to know why/how class A is able to access the private field 'varB' of its inner class B. I assume that the field 'varB's scope would be confined to that of class B. Shouldn't the behaviour have been consistent with trying to access the private field 'varC' of class C from within a member function of class A?
Thank you for your time.
[EJFH: Added "CODE" tags to preserve formatting so code nesting is clearer. ] [ October 07, 2006: Message edited by: Ernest Friedman-Hill ]
Inner classes have a special relationship to their enclosing classes, especially they have access to outer private members.
Correct me if I'm wrong: This access to private members is the reason why inner classes were introduced into Java.
all events occur in real time
Joined: Oct 07, 2006
Thanks for the welcome Bu
Like you mentioned, the inner classes have an implicit link to the enclosing class. Hence it is able to access the fields of the enclosing class (including private). However, what I don't get is how can the enclosing class access a private field of its inner class?
However, what I don't get is how can the enclosing class access a private field of its inner class?
Well, logically, it can because the language specification says so...
Technically, the compiler generates a synthetic getter and/or setter for the field that the outer class than uses to access it.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
From JLS3 6.6.1: "Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (�7.6) that encloses the declaration of the member or constructor." Since a nested class is, by definition, nested inside a top-level class, it is within the body of that top-level class and can access any private member or constructor within that same top-level class.
Now the specification doesn't say how this should happen - that's not really the job of a specification. It's really up to the implementation programmers to make it happen, and those details are not documented in the current specs. However if you're interested, you can track down the original Nested/Inner Classes Specification (they never did decide which name to use before it was released). You can download it as part of the JDK 1.1 docs in the archive area here. The section on "How do inner classes work?" will give you some of the details - though it's possible some implementation details have changed since the original release of this specification.
Also: I can't tell from EFH's message above if "Johnny Augustus" is the name you had when you first started this thread, or if you've changed it. But it doesn't strike me as a very real-looking name. Care to try again? We are serious about the display name policy.