Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could you help me find a more elegant way?

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading