• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum

 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is the time to use Enums?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean enums or the Enum class.

There is a class called Enum which is rather like Object in that it is the superclass of all enums. It has about two methods which are not in Object, and a constructor which you are not able to use. Look it up in the API specification.

There is a thing called an enumerated type which is the opposite end of the spectrum from interfaces. You know you have interfaces which have nothing implemented, then abstract classes with parts implemented, then concrete classes with everything implemented?
Then you have members of enumerated types which have everything implemented and implicitly final, which makes them a sort of "read-only" object.

An enumerated type is a group of objects which have attributes, but there are implicitly final, so it is rather like a read-only file. You can set up constructors for them and give them attributes and methods, but not change the attributes. There is an example in the Java Tutorial for planets, which is easy to understand. Another potential enumerated type might look like this:
When would you want to use them? Whenever you need a group of objects with read-only attributes.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They provide for stronger type-checking.
If your intent is (for instance, using the above example) Day of the Week.

Using constants:

public static final int MONDAY = 1;
...

Is bad coding style (IMHO), when you could use enum or Enum.

I would also suggest you use the Enum class (until you know the difference, and know that enum will give you something Enum won't - which is possible but unlikely).


If there is a set in stone, finite number of elements for a type, it's something you need to strongly consider. The larger the collection, the more unwieldy.

If it's something that's likely to change over the lifetime of your program, you should use some other data structure. The days of the week won't change, but perhaps a list of valid colors for the exterior of a car might.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another big advantage of enum values over integer constants is that they are still objects, and can have methods too.

Now you can mimic most of the enum behaviour by having a final class with a private constructor and only a precreated set of static final instances, but enums are 1) much easier, since they do all the hard work for you, including serialization, and 2) they can be used in switch statements.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic