text: A short primer on Java enums - part 1.
  Introducing Java enums);
  Listing enum values.
  Conclusion
  So now, we've covered some of the basic uses of enums. In the next part of this article, we will look at more advanced uses of enums, such as how they integrate with Hibernate.
  - Login or register to post comments
  - Printer-friendly version
  - johnsmart's blog
  - 7355 reads
  by danvanatta - 2009-05-07 10:55Similar to tusca, but still avoiding the IllegalArgumentException, and avoiding a switch statement, you can use this syntax: public enum Status { OPEN { boolean isOpen() {return true; } }, CLOSED { boolean isOpen() {return false; } }, REOPENED { boolean isOpen() {return true; } }; abstract boolean isOpen(); } This syntax is based off of (near the bottom of the page, see the Operation enum)
  Indeed Enum are more versatile than one can think of , see ...
  by javinpaul - 2011-08-19 23:39
  Indeed Enum are more versatile than one can think of , see the below post 10 examples of enum in java to know what else can you do with enum in java.
  by tusca - 2008-05-18 23:59if the Open status and the ReOpened status both mean that the status "is Open" then maybe add that meaning to the enum itself, and use isOpen(). public class Issue { public enum Status { Open(true), Closed(false), Reopened(true); private boolean open; private Status(boolean open) { this.open=open; } public boolean isOpen() { return open; } }; private Status status; public Status getStatus() { return status; } public void setStatus(Status status) { this.status = status; } public boolean isOpen() { return this.status.isOpen(); } }
  by amalter - 2008-05-17 10:14re bironran Wouldn't that could be simpler like this: public boolean getStatusSimple(Status status) { assert(status != null); switch (status) { case CLOSED: return true; case OPEN: return false; case REOPENED: return true; default: throw new IllegalArgumentException("Invalid Status"); } } Although, I'm surprised you need the assert. I would have handled null status cases in code well before we got to a method like this. Just like you did with your int case. I would think it's fair to assume that Status can only be NonNull. But.. I guess depending on your feelings on that JSR (what was it, 308 or something) - A @NonNullable annotation or such could be added and then you can take the assert out.
  by bironran - 2008-05-17 02:37Java enums are great, but switching on them is painful.
  When receiving an int you could use the following syntax: public boolean getStatus1(int status) { switch (status) { case STATUS_OPEN: return true; case STATUS_CLOSED: return false; case STATUS_REOPENED: return true; } throw new IllegalArgumentException("status must be valild"); } which is all well and nice.
  However, when you move to enums, you first code it like this (at least I did): public boolean getStatus2(Status status) { switch (status) { case CLOSED: return true; case OPEN: return false; case REOPENED: return true; } } Oh what a surprise - this doesn't even compile!
  So you add a "throw new IllegalArgumentException("status must be valid");" after the switch, compile it and run it.
  ...
  and get an NPE for all your effort.
  You revise the code again to this: public boolean getStatus3(Status status) { if(status != null) { throw new IllegalArgumentException("status may not be null"); } switch (status) { case CLOSED: return true; case OPEN: return false; case REOPENED: return true; } throw new IllegalArgumentException("status must be valid"); } Compared with the first "int" code, I'd say the final enum version has about 100% LOC overhead.
  Now, enums can't be extended, can't be effectively type-casted (because they extend Object and nothing can extend them). However, for some reason, they can be null and, maybe even worse, the compiler thinks they can be of other values than stated at compile time (which is true, if you compile with one version and run with another, but that's true for all classes).
  This leads to a huge overhead above what you had to write when naively using "int" as the enum, not to mention "int" let you combine flags (assuming you using the binary notation) and there is no such option for enums (other then create a set of enums - Set in your example - and iterating over it), which adds even more overhead to the switching method.
  All in all, I like enums, but hate the way they're implemented.
  by tiffytiffyteapot - 2008-05-14 18:49Get a Gateway.
  by richunger - 2008-05-14 22:23One of the most interesting parts of his talk, I thought, was his use of an enum with one element as a way of providing a serializable singleton.