Skip to content
Home » How to Create a Valued Enum in Java

How to Create a Valued Enum in Java

black coffee beans on white surface

Here’s how to create a valued Enum in Java.

public class ValuedEnum {

    public static void main(String[] args) {
        for (Gender gender : Gender.values()) {
            System.out.printf("The value of %s is %s%n", gender, gender.getValue());
        }
    }

    public enum Gender {
        FEMALE('f'),
        MALE('m');

        private final char value;

        Gender(char value) {
            this.value = value;
        }

        public char getValue() {
            return value;
        }
    }
}

If you have any questions, leave a comment or ask me on my social media.