How to get all enum values in Java?

153,733

Solution 1

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

Object[] possibleValues = enumValue.getDeclaringClass().getEnumConstants();

Solution 2

YourEnumClass[] yourEnums = YourEnumClass.class.getEnumConstants();

Or

YourEnumClass[] yourEnums = YourEnumClass.values();

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
153,733
Roman
Author by

Roman

If you need some help or advice in : Distributed system Cloud computing Very Large Datasets Everything that has any relations to Java and Jvms I might help :-)

Updated on January 19, 2022

Comments

  • Roman
    Roman 11 months

    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 ?

  • ColinD
    ColinD almost 13 years
    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.
  • Peter Kriens
    Peter Kriens almost 6 years
    Why do you use the getDeclaringClass()?
  • ColinD
    ColinD almost 6 years
    @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.
  • Peter Kriens
    Peter Kriens almost 6 years
    Thanks! I had not realised that case but you are right, the constant can be of an anonymous inner class.
  • zeratul021
    zeratul021 over 2 years
    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.