开发者

How do I use an Enum in another Java class?

开发者 https://www.devze.com 2023-03-26 17:19 出处:网络
I have a Java file which is solely an enum: public enum MyEnum { ONE, TWO, THREE; } And I want to开发者_StackOverflow中文版 access this enum in a different file e.g. MyClass.java. Do I use MyEnum.T

I have a Java file which is solely an enum:

public enum MyEnum {
    ONE, TWO, THREE;
}

And I want to开发者_StackOverflow中文版 access this enum in a different file e.g. MyClass.java. Do I use MyEnum.TWO, or what?


Exactly. You can do such:

MyEnum x = MyEnum.TWO;

Don't forget to import MyEnum if it is in another package.


Yes. for example:

MyEnum enumVar = MyEnum.TWO;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号