Why can't enumerations be declared locally in a method?

Today I found myself coding for something like this ...

public class LocalEnums { public LocalEnums() { } public void foo() { enum LocalEnum { A,B,C }; // .... // class LocalClass { } } } 

and I was surprised when the compiler reported an error on the local enum :

Member enum LocalEnum cannot be local

Why can't enums be declared locally, for example classes ?

I found this very useful in certain situations. In the case where I was working, the rest of the code did not need an enum value.

Is there any construct / construct conflict that explains why this is not possible, or could this be a future Java feature?

+48
java enums
Mar 31 '09 at 13:02
source share
5 answers

Enumerations are static nested classes because they define static member variables (enumeration values), and this is not allowed for inner classes: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8 .html # jls-8.1.3

Update: I looked at JLS (the Java language specification) for more details on the limitations of static nested classes and did not find it (although there is probably another topic there). From a pure point of view, there is no reason why this cannot be done. Therefore, I suspect that this is a problem of linguistic philosophy: it should not be done, therefore, it will not be supported. But I was not there, so pure speculation.

As a comment: if your methods are large enough to require their own enumerations, then this is a strong sign that you need refactoring.

+36
Mar 31 '09 at 13:05
source share

I rarely find that I am writing any types inside a method unless it is an anonymous inner class. However, you can write nested enumerations:

 public class NestedEnum { private enum MyEnum { X, Y, Z } public void foo() { } } 

I don’t think I would really like to read a method that declares a new type inside it - do you have any specific reason for wanting to declare it inside the method, and not as a nested type? I can see the argument β€œthere are no other methods you need to know,” but I think a comment can sort and leave more readable code.

+14
Mar 31 '09 at 13:06
source share
  • "Nested enumeration types are implicitly static." 8.9 Enums

  • It is reasonable to conclude that nested enumeration types implicitly contain a static access modifier.

  • β€œThis is a compile-time error if a local class declaration contains any of the following access modifiers: public, protected, private, or static.” 14.3 14.3 Local Class declarations
+9
May 26 '10 at 5:23
source share

This is strange because the definition of the Java inner class says that compile-time constants can be declared static, and the Enum member is explicitly a compile-time constant, plus enum is a static class, presumably ...

Documentation :

8.1.3 Enclosing inner classes and instances

(...) Inner classes cannot declare static members unless they are constant compile-time fields.

 class Outer{ class Inner extends HasStatic{ static final int x = 3; // ok - compile-time constant static int y = 4; // compile-time error, an inner class } static class NestedButNotInner{ static int z = 5; // ok, not an inner class } interface NeverInner{} // interfaces are never inner } 
+3
Apr 01 '09 at 4:41
source share

http://mindprod.com/jgloss/enum.html gives a good description of java enumerations - as mentioned earlier, enumerations are defined as static, so they cannot be declared as local residents

+2
Mar 31 '09 at 13:23
source share



All Articles