I'm reading J.Bloch's Effective Java and come up with the following (Setcion about enums):
Far fewer enums benefit from associating multiple behaviors with a single method. In this relatively rare case, prefer constant-specific methods to enums that switch on their own values
To me this case of using compile-time constants is not clear. Couldn't you provide a simple example reflecting that?
I think what is being discussed in that passage is the comparison between:
void myMethod(MyEnum enum){
switch(enum){
case VALUE1:
break;
case VALUE2:
break;
}
}
and
enum MyEnum{
VALUE1(){
protected void myMethod(){
//body
}
},
VALUE2(){
protected void myMethod(){
//body
}
}
protected abstract void myMethod();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With