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?
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).
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.
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).
Every enum constant is static. If we want to represent a group named constant, then we should go for Enum.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With