The enum Class in Java


Enums restrict a variable to have one of only a few predefined values, improving type safety.

Source Code

enum Size {
    SMALL, MEDIUM, LARGE, EXTRALARGE
}

public class Test {
    Size pizzaSize;
    
    public Test(Size pizzaSize) {
        this.pizzaSize = pizzaSize;
    }

    public void orderPizza() {
        switch (pizzaSize) {
            case SMALL:
                System.out.println("Ordered a small size pizza.");
                break;
            case MEDIUM:
                System.out.println("Ordered a medium size pizza.");
                break;
            // Handle other cases
        }
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments