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

jshell reports error on initialisation of static final field of anonymous class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 20
    • 19
    • tools
    • None
    • b18

      File: Main.java

      public class Main {
        public static void main(String[] a) {
          var obj = new Object() {
            public static final String msg = "Hello";
          };
          System.out.println(obj.msg);
        }
      }

      $ java Main.java
      Hello

      $ jshell
      | Welcome to JShell -- Version 19
      | For an introduction type: /help intro

      jshell> var obj = new Object() {
         ...> public static final String msg = "hello";
         ...> }
      | Error:
      | cannot assign a value to final variable msg
      | public static final String msg = "hello";
      |

            jlahoda Jan Lahoda
            sundar Sundararajan Athijegannathan
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: