Search code examples
javaenums

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;
}

Solution

  • public enum EXIT_CODE {
        A(104), B(203);
    
        private int numVal;
    
        EXIT_CODE(int numVal) {
            this.numVal = numVal;
        }
    
        public int getNumVal() {
            return numVal;
        }
    }