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

Confusing diagnostic for assigning a static final field in a constructor

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 21
    • None
    • tools
    • None
    • b09

      The diagnostic for the following ill-formed program is potentially confusing, since the diagnostic only mentions that the fields is final (not that it is static), and initializing non-static final fields in the constructor is legal.

      Something like the following might be make the error easier to understand:

      > cannot assign a value to static final field g; did you mean to initialize it in a static initializer block or static field initializer?

      ```
      public class T {
        private static final boolean g;
        T(boolean g) {
          this.g = g;
        }
      }
      ```

      $ javac T.java
      T.java:4: error: cannot assign a value to final variable g
          this.g = g;
              ^
      1 error

            acobbs Archie Cobbs
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: