6

Why am i getting an error "Constructor is undefined" is it in my eclipse IDE? is there something wrong with my code?

public enum EnumHSClass {
    PALADIN ("Paladin"),ROUGE("ROUGE");
}
5
  • What do you expect the ("Paladin") and ("ROUGE") to mean?
    – user253751
    Jul 28, 2014 at 7:46
  • i would want the PALADIN to have a string value of "Paladin". how could i get this kind of error where i think my syntax is right
    – Chizbox
    Jul 28, 2014 at 7:48
  • its returning me "The constructor EnumHSClass(String) is undefined"
    – Chizbox
    Jul 28, 2014 at 7:49
  • @Chizbox That is quite a useful error message I would say.
    – JamesB
    Jul 28, 2014 at 7:54
  • enum E { Palading, Rouge }; E.valueOf("Paladin").toString() might just do for a token with conversion to and from String.
    – Joop Eggen
    Jul 28, 2014 at 8:03

5 Answers 5

13

If you expect your enums to have parameters, you need to declare a constructor and fields for those parameters.

public enum EnumHSClass {
    PALADIN ("Paladin"),ROUGE("ROUGE");
    private final String name;
    private EnumHSClass(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
4

You need to provide a constructor in your enum like:

public enum EnumHSClass {

    PALADIN("Paladin"), ROUGE("ROUGE");

    String value;

    EnumHSClass(String value) {
        this.value = value;
    }
}

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.

Ref : http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

2
  • Note, the constructor is not allowed to be public.
    – JamesB
    Jul 28, 2014 at 7:50
  • @JamesB Yes, and there isn't any public modifier used in constructor here. Jul 28, 2014 at 7:55
3

Enums have constructors too, but only with either private or default visibility:

public enum EnumHSClass {
    PALADIN ("Paladin"),ROUGE("ROUGE");
    private EnumHSClass(String s) {
        // do something with s
    }
}

You may want to declare a field and create a getter for it, and set the field in the constructor.

Also note that the name of the enum instance is available for free via the (implicit) name() method that all enums have - maybe you can use that instead.

0
2

Your code should look like this:

public enum EnumHSClass {

    PALADIN ("Paladin"), ROUGE("ROUGE");

    private String name;

    private  EnumHSClass(String name) {
        this.name = name;
    } 
}
2
  • 1
    This will not compile.
    – user253751
    Jul 28, 2014 at 7:49
  • For some reason I can't undo my downvote, even though you edited the question. Sorry about that.
    – user253751
    Jul 28, 2014 at 10:21
1
public enum Days {
    MONDAY(1), TUESDAY(2);
    int val;
    Days (int val) {
        this.val = val;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.