Or, to re-phrase it: can enum types be "mutable"?
public enum Foo {
ONE,TWO;
private String bar;
Foo() { this.bar = ""; }
String bar() { return bar; }
// legal?
void bar(String bar) { this.bar = bar; }
}
I guess if I want to modify it, it's no longer an enum type.
Thoughts?
It's absolutely valid. It's just a really bad idea. Callers are likely to expect the enum to be properly immutable. In some cases you might want to make it "appear" to be immutable, e.g. with caching, while still mutating the internal variables... but that's very much an edge case.
As to why Java lets you do this... even if it forced all member variables to be final, that wouldn't make enum values truly immutable... for example, you could have a List<String>
which was modified each time you called a particular method...
Fundamentally, Java's not very good at enforcing immutability.