Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java how to access inner enum class

Tags:

java

enums

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.

like image 930
Terry Li Avatar asked Jun 08 '11 15:06

Terry Li


People also ask

Can an enum be an inner class?

Yes, we can define an enumeration inside a class.

How do you access methods inside an enum?

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.


1 Answers

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(); 
like image 116
Matt Ball Avatar answered Oct 02 '22 19:10

Matt Ball