17
class Coffee{   
   enum CoffeeSize{BIG,HUGE,OVERWHELMING}   
   CoffeeSize size;   
  }   

class CoffeeTest{   
   public static void main(String[] args)   
  {   
       Coffee drink=new Coffee();   
       drink.size=Coffee.CoffeeSize.BIG;   
   }   
} 

Coffee.CoffeeSize.BIG: i can get CoffeeSize just using the class name Coffee. Am I correct when I think the enum type is implicitly static?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
saravanan
  • 5,339
  • 7
  • 45
  • 52
  • 2
    When you think about what enums represent and how they are supposed to be used, you have to admit they can only be static. – biziclop Jan 28 '11 at 10:53
  • Rephrased this to make it a question – Sean Patrick Floyd Jan 28 '11 at 11:46
  • It appears we missed the same question with the same answer on the self check for chapter 1 :( After review, this should have been obvious considering they can't be instantiated.. – Whired Apr 21 '12 at 21:04
  • **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

20

Yes, it is. The language specification even says so. From the JLS section 8.9 (enums):

Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static.

reevesy
  • 3,452
  • 1
  • 26
  • 23
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194