Skip to content
Advertisement

Is it possible to assign numeric value to an enum in Java?

Is anything like this possible in Java? Can one assign custom numeric values to enum elements in Java?

public enum EXIT_CODE {
    A=104, B=203;
}

Advertisement

Answer

public enum EXIT_CODE {
    A(104), B(203);

    private int numVal;

    EXIT_CODE(int numVal) {
        this.numVal = numVal;
    }

    public int getNumVal() {
        return numVal;
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement