Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Enum Constructor Undefined

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");
}
like image 992
Chizbox Avatar asked Jul 28 '14 07:07

Chizbox


Video Answer


2 Answers

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;
    }
}
like image 147
QBrute Avatar answered Oct 10 '22 11:10

QBrute


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

like image 4
Shail016 Avatar answered Oct 10 '22 13:10

Shail016