views:

156

answers:

4

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.

+6  A: 

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.

CD Sanchez
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
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
@gustafc: my sentiments exactly!
Anita
A: 

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();
        System.out.println(m.getTest1());
    }
}

Mondain
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.
Anita
What I meant is, your code doesn't have something that looks like <pre>Test();<pre> in it.
Anita
In that case it cant have a public ctor since it is itself "private", it can be default however like so: http://ideone.com/90b3o
Mondain
+8  A: 

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.

jjnguy
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.
Anita
@Anita, are you sure default is allowed?
jjnguy
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?
Anita
@Anita See Daniel's answer. "You cannot invoke an enum constructor yourself."
ColinD
@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
+1  A: 

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.

harto