This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enum" Watch "Enum" New topic


Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 169
When is the time to use Enums?

Varuna Seneviratna
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
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

Joined: Nov 08, 2007
Posts: 457
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.

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Enum
It's not a secret anymore!