4 Replies Latest reply on Sep 22, 2008 5:08 PM by mlk

    static enum { ... } vs. enum { ... } in a class

    807589
      There is a thing about enum declared in a class like an inner class/static nested class that I not am sure about. Look at the code sample below, are there any difference if the enum is declared as static enum { ... } or as enum { ... } (not static!) and if so, what is the difference?

      If the enum is compared to a static nested class would the difference be about what it may access from its instance methods, the doStuff in the enum below. I have heard someone say that it works like a static nested class does. But there is no difference that I can see. And the compiler accepts both static and non static enum. If there is a difference may it be good and maybe also important to know about it.
      class EnumTest
      {
          static enum InnerScopedEnum
          {
              A,
      
              B,
      
              C;
      
              public void doStuff()
              {
                  // Cannot make a static reference to the non-static
                  // method instanceMethod() from the type EnumTest
                  // instanceMethod();
      
                  // Cannot make a static reference to the non-static
                  // field instanceVariable
                  // instanceVariable = 3;
      
                  classMethod();   // OK
                  classVariable = 3;  // OK
                  
              }
          }
      
      
          private void instanceMethod()
          {
              EnumTest.InnerScopedEnum.A.doStuff();   // OK
      
              System.out.println("instanceMethod");
          }
      
      
          static void classMethod()
          {
              EnumTest.InnerScopedEnum.A.doStuff();   // OK
      
              System.out.println("classMethod");
          }
      
      
          private int instanceVariable = 5;
      
          private static int classVariable = 5;
      }
        • 1. Re: static enum { ... } vs. enum { ... } in a class
          mlk
          Taking a look over the JSL, it would appear that enums could not be inner classes as "Inner classes may not declare static members" [§8.1.3|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3].
          Scanning [§8.9|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#301020] and I found: "Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static."
          • 2. Re: static enum { ... } vs. enum { ... } in a class
            807589
            mlk wrote:
            enums could not be inner classes
            No, enums are not normal classes, and compared to "inner classes" is it what thy call static nested classes that is most close. But it is still in a way a class, one that not may be explicitly instantiated and sub classed, but one that may have its own static and/or instance methods. I often use enums with methods.

            I have not looked in JLS very much, I actually thought it was a bit out-of-date in lack of maintenance, but it is apparently not. And "Nested enum types are implicitly static" seems to be how to treat static vs. non static enums. That is they are always static whether declared so or not. Like methods in an interface that always are public static whether declared that or not. That would then make the rules clear, they may only access static members in their enclosing class like a static nested class. Which also is how it works in my little code sample above.

            I then guess this question is answered now, and thanks for your reply!
            • 3. Re: static enum { ... } vs. enum { ... } in a class
              807589
              JonnyAndersson wrote:
              I have not looked in JLS very much, I actually thought it was a bit out-of-date in lack of maintenance, but it is apparently not.
              For your penance, read one section each night before bed, for a week.
              • 4. Re: static enum { ... } vs. enum { ... } in a class
                mlk
                JonnyAndersson wrote:
                No, enums are not normal classes,
                Enums are classes with knobbly bits. On of the knobbly bits is they have a bunch of static constants. Inner classes can not have static members. Thus a enum can not be an inner class.
                bit out-of-date in lack of maintenance,
                No and if they were it would be a very bad thing.