在类中声明的枚举类型是否隐式静态? [英] Are enum types declared in a class implicitly static?

查看:100
本文介绍了在类中声明的枚举类型是否隐式静态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CoffeeSize {BIG,HUGE,OVERWHELMING}
CoffeeSize size;
CoffeeSize size;
}

class CoffeeTest {
public static void main(String [] args)
{
咖啡饮料=新咖啡();
drink.size = Coffee.CoffeeSize.BIG;
}
}

Coffee.CoffeeSize.BIG :我可以得到 CoffeeSize 只需使用类名称 Coffee 。我认为枚举类型是隐式静态的我是否正确?

解决方案

是的,是的。语言规范甚至这样说。从 JLS第8.9节(枚举)


嵌套枚举类型是隐式静态的。明确声明嵌套的枚举类型为静态是允许的。



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?

解决方案

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.

这篇关于在类中声明的枚举类型是否隐式静态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆