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.
Yes, we can define an enumeration inside a class.
Every enum constant is always implicitly public static final. Since it is static, we can access it by using the enum Name. Since it is final, we can't create child enums. We can declare the main() method inside the enum.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With