Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4276296

Switch/Case should allow for looser definition of "Constant"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.2.2, 6u21
    • specification
    • generic, x86
    • generic, windows_7

      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)
      ======================================================================

            abuckley Alex Buckley
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: