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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • None
    • 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.

            dlsmith Dan Smith
            shade Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: