• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class - private field access

 
Johnny Augustus
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Johnny Augustus
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Johnny Augustus
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic