39
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.

1
  • Constant.Status status = Constant.Status.NORMAL; doesn't work ? Commented Jun 8, 2011 at 15:06

3 Answers 3

53

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();
Sign up to request clarification or add additional context in comments.

1 Comment

if an enum is a member of a class, it is implicitly static - there's no need for the static keyword.
6

In your code just do:

Constant.Status.ERROR.toString();

Comments

2

Since this was not mentioned before, in the original question the enum has the public access modifier which means we should be able to do Constant.Status.ERROR.toString() from anywhere. If it was set to private it would have been available only to the class: Constant. Similarly it is accessible within the same package in case of no modifier (default).

1 Comment

What 'outer enum'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.