Junior — Middle
Explain the concept of Enum Class and how it differs from regular enumerations.
sobes.tech AI
Answer from AI
In Android (and Java in general), enum class is an enumeration class that represents a set of constants with the ability to add fields, methods, and constructors.
Differences from regular enumerations (for example, in C or simple constants):
- Type safety — enum is a full-fledged class, and a variable of enum type can only accept the specified values.
- Ability to add behavior — methods and fields can be defined inside the enum.
- Use in switch — enum is convenient to use in selection statements.
Example of enum in Java/Android:
public enum Status {
NEW(0),
IN_PROGRESS(1),
DONE(2);
private int code;
Status(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
Thus, enum class is an extended way of defining a fixed set of constants with additional functionality, unlike simple enumerations, which can be just a set of numbers or strings without logic.