tags:

views:

300

answers:

4

This code is taken from a SCJP practice test:

 3. public class Bridge { 
 4.   public enum Suits { 
 5.     CLUBS(20), DIAMONDS(20), HEARTS(30), SPADES(30), 
 6.     NOTRUMP(40) { public int getValue(int bid) { 
                        return ((bid-1)*30)+40; } }; 
 7.     Suits(int points) {  this.points = points;  } 
 8.     private int points; 
 9.     public int getValue(int bid) { return points * bid; } 
10.   } 
11.   public static void main(String[] args) { 
12.     System.out.println(Suits.NOTRUMP.getBidValue(3)); 
13.     System.out.println(Suits.SPADES + " " + Suits.SPADES.points); 
14.     System.out.println(Suits.values()); 
15.   } 
16. }

On line 8 points is declared as private, and on line 13 it's being accessed, so from what I can see my answer would be that compilation fails. But the answer in the book says otherwise. Am I missing something here or is it a typo in the book?

+2  A: 

First check out line 12

  System.out.println(Suits.NOTRUMP.getBidValue(3));

getBidValue is undefined

Cesar
it is defined at line 6
Vasil
@Vasil: on line 6 it says "getValue" not "getBidValue"
newacct
sorry, so there is a typo after all. +1 for noticing that.
Vasil
This code appears in this book: http://amazon.co.uk/dp/0071591060But in my book copy line 12 is: System.out.println(Suits.NOTRUMP.getValue(3));I don't find any typo in this code.
aovila
@aovila I have only an electronic version of the book and I copied and pasted, maybe it's corrected in print
Vasil
@Vasil why not edit your question so theres no more typos in there...
mP
@Vasil my print and electronic versions have the same line I wrote before, maybe your copy is out of date. Be careful, maybe your copy has more typos like this!
aovila
+8  A: 

All code inside single outer class can access anything in that outer class whatever access level is.

stepancheg
+3  A: 

To expand on what stepancheg said:

From the Java Language Specification section 6.6.1 "Determining Accessibility":

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 that encloses the declaration of the member or constructor.

Essentially, private doesn't mean private to this class, it means private to the top-level class.

newacct
A: 

Similarly, an inner class can access to private members of its outer class.

aovila