Why do we need (i.e. generally advised) to use 'private' constructors for 'enum' types in Java (1.5)? .... Then, why do we have the option to use public constructors?
Enums in 1.5 are just a special type of class. Because there is no way to limit the visibility of a constructor in any other class it was to difficult, without enough benifit, to force the constructors to be private.
Originally posted by Mallesham Karnati: Why do we need (i.e. generally advised) to use 'private' constructors for 'enum' types in Java (1.5)? .... Then, why do we have the option to use public constructors?
states Enum protected Enum(String name, int ordinal) Sole constructor. Programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum type declarations. ".
Originally posted by Santana Iyer: Enum constructor is private so that we do not instantiate may be. But check it out even if you do not keep it private it should work.
The constructor which Santana was saying is the one that is declared inside the enum block, not the protected constructor of Enum class defined in the API. The constructor for an enum type is implicitly private; that is, with or without the modifier "private" being specified, the constructor is always private.
For example:
If a modifier other than "private" is placed before Day constructor, a compiler error will be generated.