Help - Using enumerations in a case statement

Discussion in 'Java' started by exquisitus, Apr 24, 2005.

  1. exquisitus

    exquisitus Guest

    I'm using JDK (1.4) and cannot use JDK 1.5 for my current project.

    I have wrapped up an enumeration in a class as ff:

    public final class myEnum {
    private String id ;
    public final int ord ;
    private static int enumCount = 0 ;
    private myEnum prev ;
    private myEnum next ;
    private static myEnum first = null ;
    private static myEnum last = null ;

    /* Enums */
    public static final myEnum ENUM_ONE = new myEnum("Enum1") ;
    public static final myEnum ENUM_TWO = new myEnum("Enum2") ;
    .....

    private myEnum(String newId) {
    this.id = newId ;
    this.ord = enumCount++ ;
    if ( first == null ) first = this ;
    if ( last != null) {
    this.prev = last ;
    last.next = this ;
    }
    last = this ;
    }
    ..... etc.
    ..... etc.

    };

    I am using this Enum class in a switch statement as follows:

    foo (MyEnum e, ....) {
    switch (e.ord)
    {
    case myEnum.ENUM_ONE.ord:
    ...
    break;

    case myEnum.ENUM_TWo_Ord:
    ...
    break;

    ......etc
    }
    }


    I get the ff compile time error: "case expressions must be constant
    expressions". I know that - but hasn't ord being initialized as a
    'constant' for each instance of my enumeration?

    What am I doing wrong ?. How can I fix it so I can use my enumerations
    in a case statement as shown in the foo() function above.

    Thanks
    exquisitus, Apr 24, 2005
    #1
    1. Advertising

  2. exquisitus

    Guest

    I think that the problem is the cases must be constants whose values
    can be determined at compile-time.
    , Apr 24, 2005
    #2
    1. Advertising

  3. exquisitus <> writes:

    > I get the ff compile time error: "case expressions must be constant
    > expressions". I know that - but hasn't ord being initialized as a
    > 'constant' for each instance of my enumeration?


    Your use of 'constant' is different from the language's use of
    'constant'. Compiler wants 'constant at compile time', you have
    'constant at runtime'.
    Tor Iver Wilhelmsen, Apr 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clintonG

    HttpStatusCode Enumerations...

    clintonG, May 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,909
    clintonG
    May 6, 2004
  2. exquisitus
    Replies:
    12
    Views:
    22,321
    Tilman Bohn
    Feb 19, 2005
  3. Gadrin77
    Replies:
    2
    Views:
    662
    Andy Fish
    May 5, 2004
  4. Martin Magnusson

    Enumerations as case labels

    Martin Magnusson, Mar 16, 2005, in forum: C++
    Replies:
    3
    Views:
    416
    Mark A. Gibbs
    Mar 16, 2005
  5. pintu

    using case statement

    pintu, Mar 12, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    401
    bruce barker
    Mar 12, 2008
Loading...

Share This Page