简体   繁体   中英

How can I get a list of all elements of a public inner enum?

Given this class:

public class Outer {

  public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
  }

}

I tried new Outer().Day.values(); and new Outer().Day.getEnumConstants(); but I'm getting Day cannot be resolved or is not a field

Any idea how I could access it?

You can access is as follows: Outer.Day.values(); The enum Day is publicly accessible just like a static variable would be.

I ran your code using IDEONE and here is my solution (adapt it to your needs):

public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
  }
    public static void main (String[] args) throws java.lang.Exception
    {
        Day[] d = Outer.Day.values();
        for (Day dd : d){
            System.out.println(dd);
        }
    }

The link is - http://ideone.com/B1qLKg

Is that what you wanted?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM