1

Java does not allow enum to be declared in a method that is a basic java syntax.

But

Could any body explain why is that, what could have gone wrong if it would have been allowed by Java, I am sure there must be some cause behind this restriction, any idea?

class Example {

    void aMethod() {

        //This is not allowed
        enum Status {
            NEW,
            PROCESSING,
            COMPLETED;

        }
    }
}
Kalher
  • 3,613
  • 2
  • 24
  • 34
  • 1
    These types of questions are fairly pointless, since noone can know the answer except for the language spec designers. I imagine though that the use of enums inside a method was deemed pointless, I can't think of a situation where it sould make sense to use it. – Keppil Feb 19 '15 at 18:33
  • Can you describe your use case when you would actually want this feature? – ControlAltDel Feb 19 '15 at 18:34
  • **Update:** Local enums, defined within a method, will be a feature in Java 16, previewing in Java 15. See: [*JEP 384: Records (Second Preview)*](https://openjdk.java.net/jeps/384). Discussed in [my Answer](https://stackoverflow.com/a/62807591/642706) on another page. – Basil Bourque Jul 09 '20 at 05:04

1 Answers1

1

enum types are typically used to share constant values between classes so declaring them in the scope of a method wouldnt make any sense

Reimeus
  • 158,255
  • 15
  • 216
  • 276