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

Lambda expression can mutate final field

    XMLWordPrintable

    Details

      Description

      The Definitely Unassigned analysis fails to detect the error in a lambda expression initializing a blank final field.

      public class LambdaFieldInit {

          private final String x;

          public LambdaFieldInit() {
              Runnable r1 = () -> x = "hi";
              x = "abc";
              r1.run();
              System.out.println(x);
          }

          public static void main(String... args) { new LambdaFieldInit(); }
      }

      Expected: compiler error in the lambda body's assignment to 'x'
      Actual: compiles, prints "hi" at runtime

      It needs to be specified that 'x' is not DU in the body of the lambda; currently, that specification is missing. See JDK-8043176.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              dlsmith Dan Smith
              Reporter:
              dlsmith Dan Smith
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: