What is enums in Java?

Enums (short for enumerations) in Java are a special data type that enables a variable to be a set of predefined constants. They provide a way to define collections of related constants in a type-safe manner. Enums enhance the readability of the code and help prevent errors by restricting the values that a variable can take.

In Java, enums are defined using the `enum` keyword, followed by a list of constants. They can also contain fields, methods, and constructors, making them quite powerful.

// Example of Enum in Java public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class TestEnum { Day day; public TestEnum(Day day) { this.day = day; } public void tellItLikeItIs() { switch (day) { case MONDAY: System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better."); break; case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so-so."); break; } } }

enums Java enums Java programming Enums in Java