Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K & B, Chapter 3, Quiz Question 7

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why we can call Suits.SPADES.points from the Bridge class.
The points variable is declared private in the Suits enum:

Any ideas?
 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question..

I know that inner classes (and since everything in java is an Object, enum is also an inner class) share a special relationship to the outer class. I know that inner classes can acces private members from the outer class, is the opposite also true?
Is this the key point? I really dont't know.



I'll stay tunned here
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Djonatah,

I don't know. There is another thing that I thought was relevant at first: Nested enums are implicitly static members of the enclosing class.
But I don't think that's the answer either.
 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben,

I tried this code in Brazil, and it worked:



It seems there's something about the special relationship between outer and inner classes. But I can't say I'm right.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Suits is an internal enum of Bridge. If you move Suits out of Bridge then it won't work any more. I.e. the following code won't compile.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thanks for checking on this, you are right. I also ran this test:


This produced the output:
2
3
1
4

So it seems an enclosing class can access the private members (both instance and static) of inner and top-level nested (a.k.a. static inner) classes. Can anyone confirm or explain? I don't remember this being mentioned before.

It makes sense, since you shouldn't deny the enclosing class the ability to access private data defined inside it (whether it's in its own code, or in classes defined inside it.)
[ December 29, 2008: Message edited by: Ruben Soto ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice research Ruben

I tried to find this in K&B and JLS but, it is not mentioned there.
Good Work

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Punit!
Ever since I have been in JavaRanch it has encouraged me to look into why things are the way the are, and all the posts and answers from people like you really help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic