Java - Enums
Enums
enum Color { RED, BLUE; }
Color c = Color.RED;
switch (c) { case RED -> System.out.println(1); case BLUE -> System.out.println(2); }
Enums can have fields and methods; use them to model finite sets of values.
Try it
- Add a field to an enum (e.g., code) and print it for each value.
- Use a switch expression on the enum to return different values.