Sub topic 5
Constant-Specific Behavior
In the last chapters, every enum constant shared the same methods. A Coin stored its own number of cents, but each coin ran the same cents() code. Sometimes each constant needs to do something different. Java lets you give every constant its own version of a method. This is called constant-specific behavior.
The Problem with One Shared Method
Say you want an enum for math operations. Each operation should take two numbers and return a result. You could write one method with a switch inside it:
enum Operation {
PLUS, MINUS;
int apply(int a, int b) {
return switch (this) {
case PLUS -> a + b;
case MINUS -> a - b;
};
}
}This works. But the logic for each constant sits in one method, away from the constant itself. If you add TIMES later and forget to add a case, the program will not compile. The compiler error points at the switch, though, not at TIMES. A cleaner option is to put each constant's code right next to its name.
Giving Each Constant Its Own Body
An abstract method is a method with a name and parameters but no body. It says "this method must exist" without saying how it works. When an enum declares an abstract method, every constant has to supply its own body for it.
A constant-specific class body is a block of code in curly braces written right after a constant's name. Inside it, the constant fills in the abstract method.
Here is a complete program you can run:
enum Operation {
PLUS {
int apply(int a, int b) { return a + b; }
},
MINUS {
int apply(int a, int b) { return a - b; }
},
TIMES {
int apply(int a, int b) { return a * b; }
};
abstract int apply(int a, int b);
}
public class Main {
public static void main(String[] args) {
for (Operation op : Operation.values()) {
System.out.println(op + " 6, 3 = " + op.apply(6, 3));
}
}
}Output:
PLUS 6, 3 = 9
MINUS 6, 3 = 3
TIMES 6, 3 = 18Look at how the code is laid out:
- Each constant is followed by
{ ... }holding its ownapplycode. - Constants are separated by commas. The last one ends with a semicolon.
- The line
abstract int apply(int a, int b);comes after the constants. It promises that every constant has anapplymethod.
The Compiler Keeps You Safe
Suppose you add a new constant and forget its body:
enum Operation {
PLUS {
int apply(int a, int b) { return a + b; }
},
DIVIDE; // no body
abstract int apply(int a, int b);
}This does not compile. The error points right at DIVIDE, so you cannot forget to give a new constant its behavior.
Mixing Fields and Bodies
You can combine both ideas. Fields hold the data, and the bodies hold the behavior:
enum Operation {
PLUS("+") {
int apply(int a, int b) { return a + b; }
},
MINUS("-") {
int apply(int a, int b) { return a - b; }
};
private final String symbol;
Operation(String symbol) {
this.symbol = symbol;
}
String symbol() {
return symbol;
}
abstract int apply(int a, int b);
}Now Operation.PLUS.symbol() returns "+" and Operation.PLUS.apply(2, 3) returns 5.
Exercises
Exercise
Add a DIVIDE constant to the Operation enum that returns a divided by b using integer division. What does Operation.DIVIDE.apply(7, 2) return?
Show answer
Add the constant DIVIDE { int apply(int a, int b) { return a / b; } }, separated from the other constants by a comma and with a semicolon after the last constant. Operation.DIVIDE.apply(7, 2) returns 3, because integer division drops the remainder.
Exercise
You add a constant named MODULO to Operation but give it no class body. What happens, and why?
Show answer
The code does not compile. Operation declares the abstract method apply, so every constant must supply a body for it. MODULO has no body, so the compiler reports an error at MODULO.