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 :  
Name: krT82822			Date: 09/28/99

According to the language specification, the "case" in a
Switch/Case statement must be a constant (i.e.
  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

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) 

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.

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

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