Enums vs static final Strings
- From: RedGrittyBrick <redgrittybrick@xxxxxxxxxxxxx>
- Date: Wed, 22 Aug 2007 11:39:03 +0100
In an earlier time-wasting diversion I wrote
private static final String THIS="This", THAT="That", OTHER="Other";
....
bp.add(makeJButton(THIS));
bp.add(makeJButton(THAT));
bp.add(makeJButton(OTHER));
....
if (OTHER.equals(command))
The repetition in the first line offends my sense of aesthetics, so I thought "why not use an enum?". I came up with this ...
private static enum ButtonName {
THIS, THAT, OTHER, MORE, ETC;
public String toString() {
return name().substring(0, 1)
+ name().substring(1).toLowerCase();
}
}
....
for (ButtonName bn: ButtonName.values())
bp.add(makeJButton(bn));
....
if (ButtonName.OTHER.toString().equals(command))
Obviously, I had to amend makeJButton to accept a ButtonName instead of a String. No problem.
However the last line now seems a bit offensive. I could hide this a bit by creating an equals() method in the enum but it still feels like a net loss to have to write "ButtonName.OTHER" instead of "OTHER".
Maybe I can console myself with the thought that my static final Strings should really have been more wordily named: BUTTON_THIS, BUTTON_THAT etc.
Is there a more concise, neater or cleverer solution?
.
- Follow-Ups:
- Re: Enums vs static final Strings
- From: Roedy Green
- Re: Enums vs static final Strings
- Prev by Date: Re: Swing GUI
- Next by Date: Re: Swing GUI
- Previous by thread: Eclipse plugin : Browser View query
- Next by thread: Re: Enums vs static final Strings
- Index(es):