Take the 2-minute tour ×

The whole question is in the title. For example:

enum enumTest {

        TYPE1(4.5, "string1"), TYPE2(2.79, "string2");
        double num;
        String st;

        enumTest(double num, String st) {
            this.num = num;
            this.st = st;

The constructor is fine with the default or private modifier, but gives me a compiler error if given the public or protected modifiers.

share|improve this question

5 Answers 5

up vote 34 down vote accepted

Think of Enums as a class with a finite number of instances. There can never be any different instances beside the ones you initially declare.

Thus, you cannot have a public or protected constructor, because that would allow more instances to be created.

Note: this is probably not the official reason. But it makes the most sense for me to think of enums this way.

share|improve this answer
It does make a lot of sense. But then why should "default" be allowed? Because then other classes in the same package can invoke the constructor too. –  kitkat Sep 8 '10 at 2:03
@Anita, are you sure default is allowed? –  jjnguy Sep 8 '10 at 2:04
Yeah. I compiled and ran the code sample in the OP. No errors. But the odd thing is I still can't manually call the constructor, even with default. I thought default means as long as you're in the same package, you can access it? Am I missing something big here? –  kitkat Sep 8 '10 at 2:11
@Anita See Daniel's answer. "You cannot invoke an enum constructor yourself." –  ColinD Sep 8 '10 at 2:15
@Anita, all interface methods are public and abstract ... so in interface Runnable ( void run ( ) ; } the run method is public and abstract. Perhaps the java designers did the same thing for enum constructors (they are private whether or not you mark them private). –  emory Sep 8 '10 at 3:19

Because you cannot call the constructor yourself.

Here is what the tutorials on Enums has to say:

Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

share|improve this answer
Taken from the link Daniel provided "Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself." Since you cannot call it yourself it doesn't matter why it can be package-private. –  Syntax Sep 8 '10 at 2:19
But on the other hand, this restriction doesn't really explain why we can't have public or protected enum constructors. It's a bit weird and inconsistent to allow two access modifiers out of four; it would make more sense to allow all access modifiers ('cause you still can't call the constructor) or just allow one (best candidates being private - to point out that this can't be accessed from elsewhere - or maybe default, to reduce code noise). –  gustafc Sep 8 '10 at 7:36
@gustafc: my sentiments exactly! –  kitkat Sep 8 '10 at 14:56

Enums contain a fixed set of values, which must all be known at compile-time. It doesn't make sense to create new literals at run-time, which would be possible if the constructor were visible.

share|improve this answer

if you want to declare enum public then save the enum file with the name of the enum.

Suppose you make an enum Book{} then save it by Book.java and make a seperate file for class

this is the only way to declare enum public.

share|improve this answer
Question was about the constructor, not the Enum themselves. –  madth3 Nov 16 '12 at 5:16

The public modifier will work for sure as seen in the tutorial http://download.oracle.com/javase/tutorial/java/javaOO/enum.html Both private and protected will also work if used within a class. I did a private version example here: http://ideone.com/mb5Le

public class Main {
    private enum Test {
        TEST1, TEST2;
    public Test getTest1() {
        return Test.TEST1;
    public static void main(String[] args) {
        Main m = new Main();

share|improve this answer
The enum in your code doesn't have a constructor though :) Did you mean getTest1 as a constructor? I can see that regular methods in enums can have any modifier they please; it's just the constructors that can't be public. –  kitkat Sep 8 '10 at 2:00
What I meant is, your code doesn't have something that looks like <pre>Test();<pre> in it. –  kitkat Sep 8 '10 at 2:12
In that case it cant have a public ctor since it is itself "private", it can be default however like so: ideone.com/90b3o –  Mondain Sep 8 '10 at 2:55
Way to be on-the-ball there to whomever down-voted this 4 years later –  Mondain Dec 13 at 1:12

Your Answer


