Update: I was looking through the JLS (java language specification) for more detail on the restrictions of static nested classes, and didn't find it (although it's probably there, hidden under a different topic). From a pure implementation perspective, there's no reason that this couldn't be done. So I suspect that it was a language philosophy issue: it shouldn't be done, therefore won't be supported. But I wasn't there, so that's pure speculation.

As a comment: if your methods are large enough that they require their own enums, then it's a strong sign that you need refactoring.

kdgregory, my method don't have to be that large to use the power of enums... Enums can simplify/clarify your code. So refactoring may not be an issue here.
–
bruno condeMar 31 '09 at 13:18

2

Philosophy is no reason to exclude a feature. Lack of usefulness, likelihood of leading to dangerous code (surprising or undefined behaviour) and technical difficulty of inclusion are valid reasons. But I don't see why any of those apply here.
–
dhardyAug 21 '13 at 10:11

3

@dhardy - Language philosophy is everything: what to include, what to exclude, and how the language looks. If you don't have a philosophy, you get PHP.
–
kdgregoryAug 22 '13 at 0:58

@dhardy Philosophy is what makes us believe that a feature is not useful, that it may lead to dangerous code, etc. This can't be evaluated objectively and Java is a tiny bit off, sometimes. +1 to kdgregory for the PHP comment.
–
maaartinusJun 17 '14 at 22:33

I don't think I'd really want to read a method which declared a new type within it - do you have any concrete reason for wanting to declare it inside the method instead of just as a nested type? I can see the "no other methods need to know" argument, but I think a comment can sort that out and still leave more readable code.

Jon, I don't often write local classes as well. But, In C# you can have something like var v = new {Prop = "Hello!!!"}; and that can be useful in certain situations. You can approximate this behavior with a local class in Java. So, if classes, why not enums?
–
bruno condeMar 31 '09 at 13:26

Private nested enums (as an enum declaration within a method probably should be) can be inlined as constants at compile time. In fact, it's possible to write the equivalent final int X = .... So whatever logic is there is broken.
–
dhardyAug 21 '13 at 10:17

3

@dhardy I 100% agree. We should be able to put enums and interfaces inside methods. My answer merely explained why local enums are forbidden by policy. I definitely don't agree with the policy.
–
emoryAug 21 '13 at 12:11

It's weird because the java inner class definition says that compile-time constants can be declared static, and a member of a Enum is clearly compile-time constant, plus enum is a static class, suposedly...