0

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}

    }
}
hc_dev
  • 8,389
  • 1
  • 26
  • 38

5 Answers5

4

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.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • Not sure they (always) "are". Enums _can_ be static nested classes. See [JLS, 8.9. Enums](https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9): "Nested enum types are implicitly static." answered in: [are enum types inside a class static?](https://stackoverflow.com/questions/663834/in-java-are-enum-types-inside-a-class-static) – hc_dev Jun 21 '23 at 21:14
4

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...
}
janos
  • 120,954
  • 29
  • 226
  • 236
1

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 {

    }
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0

The member enum Day can only be defined inside a top-level class or interface

You can initialize it before main().

Shivam
  • 649
  • 2
  • 8
  • 20
0

I want to reinstate the case of public enums.

Explanation of your compiler-error

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).

Enums can be public

I saw the answer of janos pointing out that

the modifier public is not allowed before enum

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".

How to fix this compiler-error

You have two alternative ways of fixing this - by extracting the enum:

  1. to a separate class and file, like you would define a public class, or
  2. to the inside of the same class as inner-class, then removing the public modifier
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}

    }
}

(1) Fixed solution: public enum as separate file

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);
    }
}

(2) Fixed solution: enum as inner-class, non-public

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);
    }
}
hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 1
    To help fixing an incorrect answer, we can comment on it. At least you should link to the "one answer". I like the short summary "enums cannot be defined inside method bodies" equivalent to [Abimaran's answer](https://stackoverflow.com/a/33430468/5730279). You could suggest an edit and improve [janos' answer](https://stackoverflow.com/a/33430530/5730279) with that summary. – hc_dev Jun 19 '23 at 18:00
  • 1
    I didn't mean to come out how you interpreted my answer. I'm new here and I don't have enough reputation to be able to comment on others' comments yet (<50). I did try. What matters is that I answered it quite concisely, if there is anymore explaination required on any point of my answer, be sure to comment it here and I will address it. Thanks. – stucknugget Jun 21 '23 at 07:03
  • See my edit and feel free to rollback. It is not as concise as yours, but cites correctly, shows which points are incorrect and illustrates that as alternative solution. By the way, I don't agree with Abimaran. Enums can be and often are [(static) nested inner-classes](https://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class). Static member variables are allowed for inner-classes. – hc_dev Jun 21 '23 at 21:02
  • It seems clear to me. Much more detailed, thanks. However, what I meant to say was enums cannot be defined inside method bodies being static nested classes themselves. – stucknugget Jun 23 '23 at 09:25