United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4276296 : Switch/Case should allow for looser definition of "Constant"

Details
Type:
Bug
Submit Date:
1999-09-28
Status:
Closed
Updated Date:
2006-11-21
Project Name:
JDK
Resolved Date:
2006-11-21
Component:
specification
OS:
generic,windows_7
Sub-Component:
language
CPU:
x86,generic
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
1.2.2,6u21
Fixed Versions:

Related Reports
Duplicate:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

Description
Name: krT82822			Date: 09/28/99


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 eval1127@eng -- the "spirit" of this request is covered elsewhere, I believe, but am filing a reference RFE just to make sure.
(Review ID: 95666) 
======================================================================

                                    

Comments
EVALUATION

This does not strike me as very realistic or well defined.

gilad.bracha@eng 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.

###@###.### 2002-02-13
                                     
2002-02-13
WORK AROUND



Name: krT82822			Date: 09/28/99


Use if/else if/else if/else if instead of switch/case.
======================================================================
                                     
2004-06-11
EVALUATION

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.
                                     
2006-11-21



Hardware and Software, Engineered to Work Together