Constructor Enum error

public enum ProductCategory {
  FOOD, BEVERAGE, DEFAULT;

private final String label;

private ProductCategory(String label){
this.label = label;
}

public String getLabel(){
        return label;
}

I want to implement method getLabel() in this enum class, but I am gettin error: "The constructor ProductCategory() is undefined".

I already have constructor that I need, what else I need to write? I tried to write default constructor but again I am getting error.

P.S. I am total beginner in java.

Jon Skeet
people
quotationmark

The only constructor you've currently got requires a string to be passed in - but all the enum values (FOOD, BEVERAGE, DEFAULT) don't specify strings, so they can't call the constructor.

Two options:

  • Add a parameterless constructor:

    private ProductCategory() {}
    

    This wouldn't associate labels with your enum values though.

  • Specify the label on each value:

    FOOD("Food"), BEVERAGE("Beverage"), DEFAULT("Default");
    

The latter is almost certainly what you want.

people

See more on this question at Stackoverflow