6

I have this enum:

public enum Digits {
    ZERO(0);

private final int number;

    private Digits(int number) {
        this.number = number;
    }

    public int getValue(){
        return number;
    }
}

And I would like to make setter in another class which can me offer following feature: - I will give it integer value (in this case, 0) and that setter will set enum ZERO to my local variable of type Digits Is that possible? Many thanks!

user5507755
  • 251
  • 1
  • 9
  • Possible duplicate of [Cast Int to enum in Java](http://stackoverflow.com/questions/5878952/cast-int-to-enum-in-java) – awesoon Jan 19 '16 at 09:42
  • I don't understand your question. Can you clarify please what do you want to achieve? – Adam Arold Jan 19 '16 at 09:42
  • 1
    One more duplicate candidate here: http://stackoverflow.com/questions/609860/convert-from-enum-ordinal-to-enum-type Please pay attention that using ordinal or numerical values of `enum` is potentially a bad idea. Why don't you use symbolic names? – nolexa Jan 19 '16 at 09:49

3 Answers3

7

It is possible, but not by invoking the enum's constructor, as it's available only within the enum itself.

What you can do is add a static method in your enum that retrieves the correct instance based on a given value, e.g. ZERO if the given value is 0.

Then you'd invoke that method in your other class when given the int argument.

Self contained example

public class Main {
    static enum Numbers {
        // various instances associated with integers or not
        ZERO(0),ONE(1),FORTY_TWO(42), DEFAULT;
        // int value
        private int value;
        // empty constructor for default value
        Numbers() {}
        // constructor with value
        Numbers(int value) {
            this.value = value;
        }
        // getter for value
        public int getValue() {
            return value;
        }
        // utility method to retrieve instance by int value
        public static Numbers forValue(int value) {
            // iterating values
            for (Numbers n: values()) {
                // matches argument
                if (n.getValue() == value) return n;
            }
            // no match, returning DEFAULT
            return DEFAULT;
        }
    }
    public static void main(String[] args) throws Exception {
        System.out.println(Numbers.forValue(42));
        System.out.println(Numbers.forValue(10));
    }
}

Output

FORTY_TWO
DEFAULT
Mena
  • 47,782
  • 11
  • 87
  • 106
1

You can do it like this:

private Digits digit;

public void setDigit(int number) {
    for (Digits value : Digits.values()) {
        if(value.getValue() == number) {
            digit = value;
        }
    }
}
Mateusz Sroka
  • 2,255
  • 2
  • 17
  • 19
1

Here is the example how to achieve what you want

public enum Digit {

    ONE(1),
    TWO(2),
    THREE(3);

    private static final Map<Integer, Digit> mappingMap = new HashMap<Integer, Digit>();

    static {
        for (Digit m : Digit.values()) {
            mappingMap.put(m.getValue(), m);
        }
    }
    private final int digit;

    Digit(int aDigit) {
        digit = aDigit;
    }

    public int getValue() {
        return digit;
    }
    public static Digit getByDigit(int aDigit) {
        return mappingMap.get(aDigit);
    }
}

This approach has better performance than iterating over all constants for large enums.

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48