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

Top-level JShell variables aren't treated as constant variables

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 20
    • tools
    • None

      Surprising error message about a constant expression:

      jshell> final int x = 1;
      x ==> 1

      jshell> switch (x) { case x:}
      | Error:
      | constant expression required
      | switch (x) { case x:}
      | ^

      jshell> void test() { final int y = 2; switch (y) { case y:}}
      | created method test()

      jshell> test()

      Per JLS 4.12.4, both 'x' and 'y' are constant variables, so should be allowed as switch labels. (Requirements: final, primitive type or string, initialized with a constant expression.)

      If possible, the translation scheme used by JShell should be updated so that javac will see 'x' as a constant variable.

            jlahoda Jan Lahoda
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: