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

"constant expression required" on valid case label

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • tools
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.5.0_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      javac fails to recognize some static final int constants as a valid constant expressions. In the below source code, I have a static final int assigned the int literal 2, which compiles fine. In contrast, the static final int is assigned the constant expression derived from the length of a static final array, and javac complains with the below error message.

      Error also tested on 1.5.0_04 with the same results.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Save provided source as ConstantExpressionCompileTimeError.java.
      cd to directory where saved.
      execute 'javac ConstantExpressionCompileTimeError.java'.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Source is JLS compliant (END_OF_WORDS is a static final int, and thus a valid constant expression for the case label). Code should compile.
      ACTUAL -
      see below error message.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      ConstantExpressionCompileTimeError.java:8: constant expression required
                              case END_OF_WORDS:
                                   ^
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class ConstantExpressionCompileTimeError {
      static final String[] words = { "one", "two", "three" };
      static final int END_OF_WORDS = words.length+1;
      static final int ANOTHER_STATIC_FINAL_INT = 2;

      void badSwitch( int id ) {
      switch( id ) {
      case END_OF_WORDS:
      System.out.println( "Can't compile me." );
      case ANOTHER_STATIC_FINAL_INT:
      System.out.println( "No problems here." );
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      One case replace the constant expressions assigned to the static final int with a manually computed value, but this requires manual updates as code changes.

            ahe Peter Ahe
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: