Thomas Alain Thomas Alain - 17 days ago 7
Java Question

Having trouble initializing enum type

I'm having trouble initializing a basic

enum
of days of the week. Here is my code:

public class Ch3_12
{
public static void main(String[] args)
{
public enum Day {MON, TUE, WED, THU, FRI, SAT, SUN}

}
}

Answer

Read the error messages the compiler gives you:

public static void main(String[] args)
{
    public enum Day {MON, TUE, WED, THU, FRI, SAT, SUN}
    ^^^^^^ Modifier "public" not allowed here
}

... so you remove the modifier:

public static void main(String[] args)
{
    enum Day {MON, TUE, WED, THU, FRI, SAT, SUN}
    ^^^^^^^^ enum must not be local
}

... so you move it out of the method scope:

enum Day {MON, TUE, WED, THU, FRI, SAT, SUN}

public static void main(String[] args)
{
    // no more compiler errors...
}