Java Reference
In-Depth Information
}
}
Constant-specific class bodies attach behaviors to the constants. The program pro-
duces the output:
java Operation 2.0 4.0
2.0 PLUS 4.0 = 6.0
2.0 MINUS 4.0 = -2.0
2.0 TIMES 4.0 = 8.0
2.0 DIVIDED_BY 4.0 = 0.5
The above pattern is much safer than using a switch statement in the base type ( Opera-
tion ), as the pattern precludes the possibility of forgetting to add a behavior for a new
constant (since the enum declaration would cause a compile-time error).
Search WWH ::




Custom Search