126

I came across this problem that I without knowing the actual enum type I need to iterate its possible values.

if (value instanceof Enum){
   Enum enumValue = (Enum)value;
}

Any ideas how to extract from enumValue its possible values ?

Line
  • 1,529
  • 3
  • 18
  • 42
Roman
  • 7,933
  • 17
  • 56
  • 72

8 Answers8

181

Call Class#getEnumConstants to get the enum’s elements (or get null if not an enum class).

Object[] possibleValues = enumValue.getDeclaringClass().getEnumConstants();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
ColinD
  • 108,630
  • 30
  • 201
  • 202
  • 1
    Why do you use the getDeclaringClass()? – Peter Kriens Jan 20 '17 at 12:26
  • 8
    @PeterKriens: Because `getClass()` on an `enum` object may return a _subtype_ of the `enum` type itself (if, say, the `enum` constant overrides a method from the `enum` type). `getDeclaringClass()` returns the `enum` type that declared that constant, which is what you want here. – ColinD Jan 20 '17 at 18:24
  • Thanks! I had not realised that case but you are right, the constant can be of an anonymous inner class. – Peter Kriens Jan 23 '17 at 13:58
126
YourEnumClass[] yourEnums = YourEnumClass.class.getEnumConstants();

Or

YourEnumClass[] yourEnums = YourEnumClass.values();
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
someone
  • 1,355
  • 1
  • 8
  • 6
16

Enums are just like Classes in that they are typed. Your current code just checks if it is an Enum without specifying what type of Enum it is a part of.

Because you haven't specified the type of the enum, you will have to use reflection to find out what the list of enum values is.

You can do it like so:

enumValue.getDeclaringClass().getEnumConstants() 

This will return an array of Enum objects, with each being one of the available options.

Draken
  • 3,134
  • 13
  • 34
  • 54
RodeoClown
  • 13,338
  • 13
  • 52
  • 56
15

values method of enum

enum.values() method which returns all enum instances.

  public class EnumTest {
        private enum Currency {
        PENNY("1 rs"), NICKLE("5 rs"), DIME("10 rs"), QUARTER("25 rs");
        private String value;
        private Currency(String brand) {
              this.value = brand;
        }

        @Override
        public String toString() {
              return value;
        }
  }

  public static void main(String args[]) {

        Currency[] currencies = Currency.values();

        // enum name using name method
        // enum to String using toString() method
        for (Currency currency : currencies) {
              System.out.printf("[ Currency : %s,
                         Value : %s ]%n",currency.name(),currency);
        }
  }
}

http://javaexplorer03.blogspot.in/2015/10/name-and-values-method-of-enum.html

FullStackDeveloper
  • 910
  • 1
  • 16
  • 40
Rajesh Dixit
  • 181
  • 1
  • 3
9

... or MyEnum.values() ? Or am I missing something?

deleted
  • 195
  • 2
  • 3
    Yes, the actual class of the enum is not available here to make a static method call on, just an instance of some subtype of Enum. – ColinD Feb 16 '10 at 14:41
7

Here, Role is an enum which contains the following values [ADMIN, USER, OTHER].

List<Role> roleList = Arrays.asList(Role.values());
roleList.forEach(role -> {
    System.out.println(role);
    });
Ashwani Sharma
  • 439
  • 1
  • 8
  • 9
  • However, this is not generic. Question is about the case when only have the Enum object. So you need to either go via declaring class or better, via EnumSet. – zeratul021 Apr 02 '20 at 12:49
6

One can also use the java.util.EnumSet like this

@Test
void test(){
    Enum aEnum =DayOfWeek.MONDAY;
    printAll(aEnum);
}

void printAll(Enum value){
    Set allValues = EnumSet.allOf(value.getClass());
    System.out.println(allValues);
}
David Lilljegren
  • 1,799
  • 16
  • 19
3

Any one who is trying to fetch all the values as list can simply do this.

Arrays.asList(YouEnumClass.values())