JDK-4276296 : Switch/Case should allow for looser definition of "Constant"
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.2.2,6u21
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic,windows_7
  • CPU: generic,x86
  • Submitted: 1999-09-28
  • Updated: 2006-11-21
  • Resolved: 2006-11-21
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
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 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.
21-11-2006

WORK AROUND Name: krT82822 Date: 09/28/99 Use if/else if/else if/else if instead of switch/case. ======================================================================
11-06-2004

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
13-02-2002