Women in Technology

Hear us Roar



Article:
  Enums in Java (One More Time)
Subject:   enum-as-objects as array-index
Date:   2003-08-08 04:08:14
From:   anonymous2
Response to: enum-as-objects as array-index

To support switch one would change the generated code to include a set of static ints.



public final class MyEnum
{
/** The internal value of this enum */
private int m_value;


/** integer versions of this enum for use in switch */
public static final int _Val1 = 0;
public static final int _Val2 = 1;


/** the finite set of enum */
public static final SleepType Val1 = new MyEnum(_Val1);
public static final SleepType Val2 = new MyEnum(_Val2);


/** Private constructor for the static instances of this enum. */
private MyEnum(int optionIntegerValue)
{
this.m_value = optionIntegerValue;
}


/** fn that might be used to get int value for 'switch' statments */
public final int ord()
{
return m_value;
}


.... lots of other methods removed ....
}



So now we can do ....


void fn(MyEnum anEnum)
{
switch (anEnum.ord())
{
case MyEnum._Val1:
;
case MyEnum._Val2:
;
default:
// oh dear
;
}
}

Full Threads Newest First

Showing messages 1 through 1 of 1.

  • enum-as-objects as array-index
    2003-08-08 04:10:37  anonymous2 [View]

    oops sorry replace SleepType in my example with MyEnum (as you can guess I already have a code generator for this stuff and I forgot to hack out and replace those two symbols)