enumerated constants - how to

hello experts,
Is there any better way to create enumerated constants in Java?
Assume I would like to create an enumeration for the list of cars I own, what I am currently doing is:
static final int FORD=1;
static final int TOYOTA = 2;
static final int JEEP = 3;

this would serve me well if the enumeration does not change over time (sell or buy cars). However, if it changes, the chance is I may have to go over the code and change some of the details to reflect the modification as well. Is there any way to create an enumeration in a more robust way?
Thanks,
Do

dttaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vn77Commented:
The problem you mention is due to the lack of type safety in the way you used to create enumeration.
The following article may have the answer for you:
http://www.javaworld.com/jw-07-1997/jw-07-enumerated.html

Hope it helps,
vn

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
expertmbCommented:
J2SE5 has enum. which version of Java  you are using.
0
petmagdyCommented:
Put those values in a Hashtable where the key is a String like "FORD" and the value is Integer like new Integer(1), just make a function like:

public int getConstantValue(String key)
{
  return Integer.intValue(hashtabel.get(key) );
}
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

vikraman_bCommented:
Hi
this will solve the type safety probs...etc
http://members.tripod.com/rwald/java/articles/TypeSafeEnumeration.html
hope this will help u
0
vikraman_bCommented:
Note that this is essentially the same technique that the java.awt.Color. class uses to define color constants.
---------------
public final class HtmlHeadingLevel {
   private HtmlHeadingLevel() {
      // no implementation is needed, yet
   }
   public static final HtmlHeadingLevel H1 = new HtmlHeadingLevel();
   public static final HtmlHeadingLevel H2 = new HtmlHeadingLevel();
   public static final HtmlHeadingLevel H3 = new HtmlHeadingLevel();
   public static final HtmlHeadingLevel H4 = new HtmlHeadingLevel();
   public static final HtmlHeadingLevel H5 = new HtmlHeadingLevel();
   public static final HtmlHeadingLevel H6 = new HtmlHeadingLevel();
}
This makes it possible to define methods that will only accept one of our enumerated values, for example:
--------------------------
public void openHeading(HtmlHeadingLevel level) {
  if( H1.equals(level) ) {
    htmloutputstream.write("<H1>");
  } else if( H2.equals(level) ) {
    htmloutputstream.write("<H2>");
  } else if( H3.equals(level) ) {
    // ...and so on...
  }
}
client
-------
myHtmlWriter.openHeading(HtmlHeadingLevel.H1);
0
vikraman_bCommented:
0
MogalManicCommented:
Your constants converted to an enumeration would be like this:
public class CarTypes
{
static final CarTypes FORD=new CarTypes ("Ford", 1);
static final CarTypes TOYOTA = new CarTypes ("Toyota", 2);
static final CarTypes JEEP = new CarTypes ("Jeep", 3);

   private String name;
   private int index;

   //NOTE: this method is private so that the only way to add new items to this class is to add it to the enumeration
   private CarTypes (String name, int idx)
   {
      this.name=name;
      this.index=idx;
   }

   public String getName() { return name;}  
   public int getIndex() { return index;}
}

This class can be used now as described in the other peoples posted links:
E.G.
/**
 *Find a available car that matches criteria
 */
public Car findCar(CarTypes type,  CarModels model, Colors color)
{
...
}


...
//Find a Red, Ford Mustang
Car foundCar=findCar(CarTypes.FORD, CarModels.MUSTANG, Colors.RED);
0
dttaiAuthor Commented:
thanks experts for your overwhelming reponses :).
I'll take some time to look at them and give you all the feedbacks.
Thanks again,
Do
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.