ENUMERATION:
- enum is the keyword.
- This datatype contains fixed set of constants
- enum is a special class we can give a name to just like other classes, but the variable will be constant.
We can use it in switch and also in
enhanced for loop.
For(classname obj: classname.values())
package Methods;
public class EnumExample {
// Define an enum for days of the week
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args) {
// Using an enum variable
Day today = Day.SATURDAY;
System.out.println("Today is " + today);
// Switch statement with enum
switch (today) {
case MONDAY:
System.out.println("It's a workday.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("It's the weekend.");
break;
default:
System.out.println("It's a weekday.");
}
// Iterating through all days using values() method
Day[] days = Day.values();
System.out.println("Days of the week:");
for (Day day : days) {
System.out.println(day);
}
}
}
0 Comments