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}
}
}
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}
}
}
You can't define an Enum inside a method.
Enums are static nested classes because they define static member variables (the enum values), and this is disallowed for inner classes.
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...
}
Member enums in Java must be defined only within a top-level class, interface or within a static context. For example:
//nested within a class
class TopLevel {
enum MyEnum { }
}
//nested within an interface
interface Interface {
enum MyEnum { }
}
Note that in both cases, MyEnum
is implicitly static
by default.
A side note: Unlike enums, however, you can nest classes within methods, which are called local classes. This is actually possible only for classes, but not interfaces and enums:
public static void main(String[] args) {
class NestedMethodClass {
}
}
The member enum Day can only be defined inside a top-level class or interface
You can initialize it before main().
I want to reinstate the case of public enum
s.
Enums cannot be defined inside method bodies.
Abimaran Kugathasan explained the reason:
Enums are static (nested) classes [which have] static variables [inside].
And
static member variables are [not allowed] for inner classes.
(I put my edits in brackets).
I saw the answer of janos pointing out that
the modifier
public
is not allowed beforeenum
This is an incorrect statement. You can definitely use public
before an enum
definition.
Therefor you have to define the enum in a separate file, like a public (non-inner) class.
See the example below in section "(1) Fixed solution: public enum as separate file".
You have two alternative ways of fixing this - by extracting the enum:
public class Ch3_12
{
public static void main(String[] args)
{
// extract this line out of the method
public enum Day {MON, TUE, WED, THU, FRI, SAT, SUN}
}
}
After extracting your enum to a separate file it looks like this:
New file Day.java
with the enum:
package my;
// extracted to a separate file, same place where you define (non-inner) public classes
public enum Day {MON, TUE, WED, THU, FRI, SAT, SUN}
Note how the public enum
is allowed when defining it outside a class.
Existing file Ch3_12.java
with your class:
package my;
import my.Day;
public class Ch3_12
{
public static void main(String[] args)
{
// use the enum here
System.out.println(Day.MON);
}
}
See a compiling and testable demo at IDEone:
class Ideone
{
// extracted to the class-level, as inner-class, then you have to drop the public modifier
enum Day {MON, TUE, WED, THU, FRI, SAT, SUN}
public static void main (String[] args) throws java.lang.Exception
{
// use the enum here
System.out.println(Day.MON);
}
}