编译器不允许使用枚举名称

时间:2014-04-17 15:29:45

标签: java

所以我在程序中声明枚举时遇到了麻烦所以我从sun网站示例中复制了代码,起初编译器比1.5更旧了所以我更新了它,错误改为:

Exception in thread "main" java.lang.NoSuchMethodError: examples.Day.values()[Lexamples/Day;
at examples.EnumTest.$SWITCH_TABLE$examples$Day(EnumTest.java:7)
at examples.EnumTest.tellItLikeItIs(EnumTest.java:15)
at examples.EnumTest.main(EnumTest.java:37)

我的代码:

import java.lang.Enum;
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY ;
}
public class EnumTest {
Day day;

public EnumTest(Day day) {
    this.day = day;
}

public void tellItLikeItIs() {
    switch (day) {
        case MONDAY:
            System.out.println("Mondays are bad.");
            break;

        case FRIDAY:
            System.out.println("Fridays are better.");
            break;

        case SATURDAY:
        case SUNDAY:
            System.out.println("Weekends are best.");
            break;

        default:
            System.out.println("Midweek days are so-so.");
            break;
    }
}

public static void main(String[] args) {
    EnumTest firstDay = new EnumTest(Day.MONDAY);
    firstDay.tellItLikeItIs();
    EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
    thirdDay.tellItLikeItIs();
    EnumTest fifthDay = new EnumTest(Day.FRIDAY);
    fifthDay.tellItLikeItIs();
    EnumTest sixthDay = new EnumTest(Day.SATURDAY);
    sixthDay.tellItLikeItIs();
    EnumTest seventhDay = new EnumTest(Day.SUNDAY);
    seventhDay.tellItLikeItIs();
}
}

1 个答案:

答案 0 :(得分:2)

你的枚举声明是错误的。将整个声明放在EnumTest类中作为内部类,删除public说明符,或将其放在单独的文件中。