Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

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

0
dttai
Asked:
dttai
6 Solutions
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now