简体   繁体   中英

How do I use an Enum in another Java class?

I have a Java file which is solely an enum:

public enum MyEnum {
    ONE, TWO, THREE;
}

And I want to access this enum in a different file eg 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM