Help - Using enumerations in a case statement

E

exquisitus

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
 
K

klynn47

I think that the problem is the cases must be constants whose values
can be determined at compile-time.
 
T

Tor Iver Wilhelmsen

exquisitus said:
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'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top