This discussion is archived
6 Replies Latest reply: Dec 4, 2006 8:16 PM by 807607 RSS

values in enum

807607 Newbie
Currently Being Moderated
I have an enum like this"
public enum HandType

               4), FLUSH(5), FULLHOUSE(6), FOUROFAKIND(7), STRAIGHTFLUSH(8);

     private Rank[] kicker = new Rank[5];
     private Rank hi;
        private Rank lo;

     private int value;

        HandType(int value)
          this.value = value;

and then I try to do this
HandType h1 = HandType.ONEPAIR;
HandType h2 = HandType.ONEPAIR;
h1.setHi(Rank.ACE); //Rank is another enum
when i run thru the debugger, after the h2.setHi() call, both h2 and h1 have their hi property as Rank.TEN. Is that correct behavior?

If h1 and h2 are different values, say ONEPAIR, and TWOPAIR, this doesnt happen.

Message was edited by:
  • 1. Re: values in enum
    796440 Guru
    Currently Being Moderated
    Yes, it's correct.

    h1 and h2 both point to the same object. There's only ever one instance of each value of the enum. That's all there can ever be. That's the way the language defines enums.
  • 2. Re: values in enum
    807607 Newbie
    Currently Being Moderated
    thats what i feared, thanx.

    I dont see this made clear at

    but on the other hand, it doesnt provide any examples of what I tried, too. I guess I need to wrap HandType in a class with the hi, lo, kicker properties.
  • 3. Re: values in enum
    807607 Newbie
    Currently Being Moderated
    It's rather implicit. If they weren't the same object it'd kind of defeat the purpose of an enumerated type. To think otherwise says to me that you either don't understand the purpose of an enumerated type or lack a fundamental understanding of the Java language.

    One thing that might be enumerated is the possible locations for an image to be painted. It might be possible to paint it in the CENTER, NORTH, WEST, EAST, or SOUTH. Here's a naive way of handling this:
    public static final int CENTER = 0;
    public static final int NORTH = 1;
    public static final int WEST = 2;
    public static final int EAST = 3;
    public static final int SOUTH = 4;
    public void setLocation(int location);
    This does nothing to prevent you from passing in -1 or 5, which aren't even valid values and may have undetermined behavior. By using a typesafe enumeration we can change this:
    public enum Location {
    public void setLocation(Location location);
    Now they must give us an instance of Location and the only instances that exist are Location.CENTER, Location.NORTH, Location.WEST, Location.EAST and Location.SOUTH. It becomes impossible to now accidentally give it an invalid value.

    Programmers were doing this before "enum" was introduced, it was just more work and couldn't be used in a switch table. The fact that the designers elected to include other nifty features, like the ability to declare and mutate other variables in the enumerated type doesn't make it a good idea to use it the way you're using it.
  • 4. Re: values in enum
    800351 Newbie
    Currently Being Moderated
    From JLS:
    An enum type has no instances other than those defined by its enum constants.

    I think your enums should be used for representing values of the attributes of your class objects.
  • 5. Re: values in enum
    807607 Newbie
    Currently Being Moderated
    I dont think it was implict kablair. Especially when this was written at the link posted above

    The new enum declaration defines a full-fledged class (dubbed an enum type). In addition to solving all the problems mentioned above, it allows you to add arbitrary methods and fields to an enum type, to implement arbitrary interfaces, and more. Enum types provide high-quality implementations of all the Object methods. They are Comparable and Serializable, and the serial form is designed to withstand arbitrary changes in the enum type.

    Ill admit I missed the boat on this one, but I am already almost completely refactored.
  • 6. Re: values in enum
    807607 Newbie
    Currently Being Moderated

    i wish i used that document instead of the one I did. I allready started exactly what you suggest.