Terry Li Terry Li - 3 months ago 22
Java Question

Java how to access inner enum class

public class Constant {

......

public enum Status {
ERROR,
WARNING,
NORMAL
}

......

}


After compiling I got a class file named Constant$Status.class. The question is, how can I access the enum value. For instance, I want to get the string representation of the ERROR status.

Answer

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();