By accident I just discovered that the Java 1.8 compiler allows the following syntax:
enum AnimalType {
DOG {
@Override
public String toString() {
return "I am a dog";
}
},
CAT {
@Override
public String toString() {
return "I am a cat";
}
public void doCatThings() {
// ...
}
},
}
Overriding toString() individually works perfectly fine. A call on an AnimalType.DOG results in the String "I am a dog".
Apart from this though, I couldn't find any information on what this enum case customization could be used for. Note the other public method for the case CAT. When defining any other public method or field, it seems like it can't be accessed from the outside anyway.
So what's the deal about this? Is this just something that is technically correct syntax but pointless?