Java enum Advanced


Enums can contain fields, methods, and constructors, making them more powerful.

Source Code

enum Day {
    MONDAY("Workday"),
    FRIDAY("Almost Weekend"),
    SUNDAY("Weekend");

    private final String type;

    Day(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

public class EnumTest {
    public static void main(String[] args) {
        for (Day day : Day.values()) {
            System.out.println(day + " is a " + day.getType());
        }
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments