Question about enums defined inside a class in java

This code is taken from the SCJP 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 declared as closed, and on line 13 it refers to it, so from what I see, my answer will be a compilation failure. But the answer in the book says otherwise. Am I missing something here or is it a typo in the book?

+6
java enums
source share
4 answers

All code within one outer class can access anything in this outer class regardless of access level.

+11
source share

To expand on what stepancheg said:

See Java Language Specification. 6.6.1. "Determination of availability" :

if the element or constructor is declared private, then access is allowed, if and only if this happens in a higher-class body that includes a member or constructor declaration.

Essentially, private does not mean private for this class, it means private for a top-level class.

+5
source share

The first line of check 12

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

getBidValue undefined

+3
source share

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

0
source share

All Articles