wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K & B, Chapter 3, Quiz Question 7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K & B, Chapter 3, Quiz Question 7" Watch "K & B, Chapter 3, Quiz Question 7" New topic
Author

K & B, Chapter 3, Quiz Question 7

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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?


All code in my posts, unless a source is explicitly mentioned, is my own.
Djonatah Stiegler
Ranch Hand

Joined: Oct 30, 2008
Posts: 32
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


From Brazil
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Oct 30, 2008
Posts: 32
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

Joined: Nov 20, 2008
Posts: 254
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.


“Everything should be as simple as it is, but not simpler.” Albert Einstein
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Oct 16, 2008
Posts: 952
Nice research Ruben

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



SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K & B, Chapter 3, Quiz Question 7