Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (10.2k points)

 use the enum to make a few constants:

enum ids {OPEN, CLOSE};

the OPEN value is zero, but I want it as 100. Is it possible?

1 Answer

0 votes
by (46k points)

If you use very big enum types then, following can be useful;

public enum deneme {

    UPDATE, UPDATE_FAILED;

    private static Map<Integer, deneme> ss = new TreeMap<Integer,deneme>();

    private static final int START_VALUE = 100;

    private int value;

    static {

        for(int i=0;i<values().length;i++)

        {

            values()[i].value = START_VALUE + i;

            ss.put(values()[i].value, values()[i]);

        }

    }

    public static deneme fromInt(int i) {

        return ss.get(i);

    }

    public int value() {

    return value;

    }

}

Related questions

0 votes
1 answer
asked Oct 23, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 23, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Aug 7, 2019 in Java by Nigam (4k points)

Browse Categories

...