• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class - private field access

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic