javac fails with "self-reference in initializer" when referencing the static field from lambda

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P2
    • None
    • Affects Version/s: None
    • Component/s: specification
    • None

      See attached test case.

      import java.util.function.Function;

      public class LambdaSelfRef {

          // COMPILATION FAILURE
          public static Function<Object, Object> op1 = e -> op1.apply(e);

          // COMPILES OK
          public static Function<Object, Object> op2 = e -> LambdaSelfRef.op2.apply(e);

          // COMPILES OK
          public static Function<Object, Object> op3 = new Function<Object, Object>() {
                  public Object apply(Object o) {
                      return op3.apply(o);
                  }
              };

          // COMPILES OK
          public static Function<Object, Object> op4 = new Function<Object, Object>() {
                  public Object apply(Object o) {
                      return LambdaSelfRef.op4.apply(o);
                  }
              };
      }

      The weird thing is that accessing the static field via class works perfectly. Seems to be constrained to lambdas, anonymous classes permit the behavior we are after.

            Assignee:
            Dan Smith
            Reporter:
            Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: