Two Laptop Bag*
The moose likes Java in General and the fly likes Inner class - private field access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inner class - private field access" Watch "Inner class - private field access" New topic
Author

Inner class - private field access

Johnny Augustus
Greenhorn

Joined: Oct 07, 2006
Posts: 18
Hi,

I have a small query regarding inner classes and access to private fields.

Eg:


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 ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Johnny,
welcome to the Java Ranch !



Johnny's code:



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.


Yours,
Bu.


all events occur in real time
Johnny Augustus
Greenhorn

Joined: Oct 07, 2006
Posts: 18
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?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Johnny Bravo:

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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real-sounding first and last name with a space between. Obviously fake names are not OK.

Please go here and fix your display name up, pronto. Thanks, pardner!


[Jess in Action][AskingGoodQuestions]
Johnny Augustus
Greenhorn

Joined: Oct 07, 2006
Posts: 18
Can you tell me where in the specification it is mentioned?

I am still not convinced why this is possible. Anyone?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class - private field access