Kifsif
Kifsif

Reputation: 3843

An enum switch case label

            LocalDate currentdate = LocalDate.now();
            Month currentMonth = currentdate.getMonth();
            FrequencyEntity frequencyEntity = null;
            switch (currentMonth) {
                case Month.JANUARY: // Here.
                    frequencyEntity = FrequencyEntity.builder().january(frequency).build();
                    
            }

The line I have commented as "Here" causes my IDE to show this: "An enum switch case label must be the unqualified name of an enumeration constant.".

enter image description here

The only possible solution I can envisage is convert the month to String. But this may be not a correct solution.

Is there a more elegant way?

Upvotes: 0

Views: 161

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502316

Note the unqualified part in the error message. It's not saying you can't use enum values as case labels - it's saying that you can't include the enum name. The code should be:

switch (currentMonth) {
    case JANUARY:
        frequencyEntity = FrequencyEntity.builder().january(frequency).build();
        break;
}

Upvotes: 4

Related Questions