-1

I prepare to scjp exam by book readind.

quote from book:

A constructor in an enum class can only be specified as private.

but I wrote code for test:

enum En{
    VAL_1;
    En(){
        System.out.println("123");
    }
    public static void main(String [] args){
    }
}

this code works for me.

Is it book bug or compiler bug ?

P.S. with protected and public access modifiers code doesn't work

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 3
    `enum` object is created by `enum` itself. You can't create it's object from outside hence there is no meaning of changing the visibility if the constructor other than `private`. You can change it but no meaning of it at all but `public` and `protected` are not allowed. – Braj Jun 01 '14 at 17:42
  • @Braj why does for package visible and for protected use difference politics ? – gstackoverflow Jun 01 '14 at 17:43
  • 1
    check this question: http://stackoverflow.com/questions/7747948/why-can-a-enum-have-a-package-private-constructor – Katona Jun 01 '14 at 17:43
  • @Katona interesting - thanks – gstackoverflow Jun 01 '14 at 17:44
  • Read it here [Java Language Specification - Enum Type](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9) – Braj Jun 01 '14 at 17:50

1 Answers1

0

In the documentation is stated that enum constructor must be package-private or private.

Enum tutorial

  • **If no access modifier is specified for the constructor of an enum type, the constructor is private** please add it to answer – gstackoverflow Jun 01 '14 at 17:47