Part 08 / Topic 10 of 12
Enums with fields and behaviour
An enum is a fixed, named set of values known completely at compile time. Part 3 mentioned in passing that a switch can check an enum, then deferred the actual type to this part — here it is. An enum is a real class, not just a list of names: each constant can carry its own fields, set through an enum constructor, and the enum can define methods every constant shares.
Think of a bank's account-type rulebook: "savings", "current" and "salary" are not just three labels, each one carries its own minimum balance and interest rule.
Real-world example: AccountType as an enum with a minimum balance and an interest rate per constant.
enum AccountType {
SAVINGS(500.0, 4.0),
CURRENT(0.0, 0.0),
SALARY(0.0, 3.0);
private final double minimumBalance;
private final double interestRatePercent;
AccountType(double minimumBalance, double interestRatePercent) {
this.minimumBalance = minimumBalance;
this.interestRatePercent = interestRatePercent;
}
double minimumBalance() { return minimumBalance; }
double monthlyInterest(double balance) {
return Math.round(balance * interestRatePercent / 100 / 12 * 100) / 100.0;
}
String describe() {
return switch (this) {
case SAVINGS -> "Savings: ₹" + minimumBalance + " minimum, interest-bearing";
case CURRENT -> "Current: no minimum, no interest, built for businesses";
case SALARY -> "Salary: no minimum while employed, interest-bearing";
};
}
}
SAVINGS ordinal=0 minBalance=₹500.0 interest(100000)=₹333.33
CURRENT ordinal=1 minBalance=₹0.0 interest(100000)=₹0.00
SALARY ordinal=2 minBalance=₹0.0 interest(100000)=₹250.00
Savings: ₹500.0 minimum, interest-bearing
valueOf("CURRENT") = CURRENT
valueOf("FOREX") -> No enum constant EnumsDemo.AccountType.FOREX
Every enum constant is built exactly once, when the class first loads, by calling the enum's own constructor with the arguments written after the constant's name — SAVINGS(500.0, 4.0) really does call AccountType(500.0, 4.0). values() returns every constant in declaration order; ordinal() is that position (starting at 0), rarely useful on its own but is what Java's enum-aware collections (EnumMap, EnumSet) sort by internally. valueOf("CURRENT") parses a String back into the matching constant and throws IllegalArgumentException, not a checked exception, for anything that does not match — useful for reading an account type back from a text file or a form, the same shape of problem as Integer.parseInt on bad input (Part 6). Part 3's arrow switch expressions always ended in a default ->, because at that point nothing told the compiler the full set of possible values. An enum is different: the compiler knows every constant that will ever exist, so an arrow switch on an enum can skip default entirely and still be exhaustive — add a fourth constant and forget its case, and javac refuses to compile describe() until you add it.
Full program: examples/EnumsDemo.java.
Done here? The practice set for Part 8 — including the solutions — is on the part page.