|
Description
|
According to the language specification, the "case" in a
Switch/Case statement must be a constant (i.e.
switch(x){
case A: //do stuff...
case B: //do other stuff...
default: //do default stuff...
} // A and B must be constant ints at compile time)
The typical way to define constant int is with a "public static final int"
declaration. However, sometimes we may want to put more "ooomf"
(such as strong typing and enumation capabilities) into our
constants and this requires creating constants of a particular
class (see the article in JavaWorld for a more in-depth explanation
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated.html).
A brief example:
public final class EmployeeType{
public final int ord;
public static int highestOrd = 0;
private EmployeeType(){
ord = highestOrd++;
}
public static final WORKER = new EmployeeType();
public static final MANAGER = new EmployeeType();
public static final SUPERVISOR = new EmployeeType();
}
We may later on try to use EmployeeType.WORKER.ord as a constant
only to find the compiler does not consider this a constant. However,
logically, it is impossible for EmployeeType.WORKER.ord to not hold
a constant value, given the code above.
------------
9/28/99 xxxxx@xxxxx -- the "spirit" of this request is covered elsewhere, I believe, but am filing a reference RFE just to make sure.
(Review ID: 95666)
======================================================================
Posted Date : 2006-11-21 23:45:30.0
|
|
Evaluation
|
This does not strike me as very realistic or well defined.
xxxxx@xxxxx 1999-09-28
On the other hand, direct addition of enum classes to the language
would enable extending the switch statement to handle enums, which
I believe is what the user really wants.
xxxxx@xxxxx 2002-02-13
Since you can use switch with enum constants, I believe the intent of this request is fulfilled. More enhancements are being considered for switch; look at the SeeAlso's.
Posted Date : 2006-11-21 23:45:30.0
|
|
Comments
|
Submitted On 30-JAN-2001
schapel
The Java switch statement is compiled into the JVM bytecode
tableswitch or lookupswitch. Both of these bytecodes require
that the case values be unique, 32-bit, integer,
compile-time constants. For this reason, case values cannot
be references, constants not known at compile time, floats,
doubles, or longs.
I suppose you could allow Java switch statement cases to
include other types of values, and compile a switch
statement that uses one of those kinds of values into other
bytecode, such as a sequence of ifs and gotos. But then we'd
have to examine all switch statements carefully in order to
determine if it will be compiled into a fast switch bytecode
or a slow sequence of if-elses.
In the end, the best solution seems to be that described in
Bug 4401321, which is an RFE for adding enums to Java.
Submitted On 12-FEB-2002
shrink_laureate
> The Java switch statement is compiled into the JVM
> bytecode tableswitch or lookupswitch. Both of these
> bytecodes require that the case values be unique, 32-bit,
> integer, compile-time constants. For this reason, case
> values cannot be references, constants not known at
> compile time, floats, doubles, or longs.
In that case, I want that fixed. The ability to switch on
dynamic values and 64-bit values should be part of the
language, be they references, longs etc. I don't care if
it's part of the bytecode or not, but we need an efficient,
non-workaround way of doing it.
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|