Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are enum types declared in a class implicitly static? [duplicate]

Tags:

java

enums

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?

like image 671
saravanan Avatar asked Jan 28 '11 10:01

saravanan


People also ask

Is enum implicitly static?

A local class declaration may not use the static keyword (14.3). Nested enum classes are implicitly declared static . A member enum class may redundantly specify the static modifier; a local enum class may not (8.9).

Are enums implicitly static and final?

An enum type is implicitly final unless it contains at least one enum constant that has a class body. It is a compile-time error to explicitly declare an enum type to be final. Nested enum types are implicitly static. It is permissible to explicitly declare a nested enum type to be static.

Are enum classes static?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Is enum always static?

Every enum constant is static. If we want to represent a group named constant, then we should go for Enum.


1 Answers

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.

like image 151
Jon Skeet Avatar answered Sep 28 '22 08:09

Jon Skeet