Enums
Represent a fixed set of values.
An enum represents a fixed list of constants.
Instead of using strings or numbers, enums make your code easier to read and less error-prone.
Creating an Enum
public enum Alliance {
RED,
BLUE
}Using an Enum
Alliance alliance = Alliance.RED;switch Statements
Enums work well with switch.
switch (alliance) {
case RED:
System.out.println("Red");
break;
case BLUE:
System.out.println("Blue");
break;
}FTC Example
Enums are commonly used for robot states.
public enum IntakeState {
INTAKING,
HOLDING,
OUTTAKING,
STOPPED
}IntakeState state = IntakeState.INTAKING;This is much clearer than using numbers like 0, 1, 2, and 3.
Key Takeaways
- Enums define a fixed set of values.
- They improve readability.
- They reduce bugs caused by invalid values.
- They work especially well with
switchstatements.
Was this resource helpful?
