Why are we using Non Modified Constructor in Enum in Java

I've seen Java code where I can see an unmodified constructor in enum

. Can someone tell me why this is so?

public enum myEnum{

  myEnum()
   {
       System.out.println("Hello World");
   }
}

      

+3


source to share


1 answer


Assuming you mean why there is no access modifier for the constructor because

The declaration enum

contains a constructor declaration without access modifiersprivate

.

and



It is a compile-time error if a constructor declaration is in an enum Declaration public

or protected

(ยง6.6).

It's superfluous to point out private

, which is why some people don't.

+12









All Articles