Java how to access inner enum class

后端 未结 3 1117
攒了一身酷
攒了一身酷 2021-02-06 23:24
public class Constant {

  ......

  public enum Status {
    ERROR,
    WARNING,
    NORMAL
  }

  ......

}

After compiling I got a class file named

3条回答
  •  死守一世寂寞
    2021-02-06 23:45

    You'll be able to access it elsewhere like

    import package.name.Constant;
    //...
    Constant.Status foo = Constant.Status.ERROR;
    

    or,

    import package.name.Constant;
    import package.name.Constant.Status;
    //...
    Status foo = Status.ERROR;
    

    To get the declared name of any enum element, use Enum#name():

    Status foo = ...;
    String fooName = foo.name();
    

提交回复
热议问题