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

Invalid method reference for this() outer instance method reference

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • 24
    • 23
    • tools
    • generic
    • generic

      This class should compile but does not:

          public class Test2 {
              class Inner {
                  Inner() {
                      this(Test2::run);
                  }
                  Inner(Runnable r) {
                  }
              }
              public void run() {
              }
          }

      $ javac -version
      javac 22
      $ javac -Xdiags:verbose Test2.java
      Test2.java:4: error: no suitable constructor found for Inner(Test2::run)
                  this(Test2::run);
                  ^
          constructor Test2.Inner.Test2.Inner() is not applicable
            (actual and formal argument lists differ in length)
          constructor Test2.Inner.Test2.Inner(Runnable) is not applicable
            (argument mismatch; invalid method reference
                unexpected instance method run() found in unbound lookup)
      1 error

      Note also that the error message says "Test2.Inner.Test2.Inner" where it should say "Test2.Inner", but that may just be a side effect of the actual bug (i.e., there shouldn't be any error message).

      Verified in JDK 8, 11, 17, 22, and 24-head.

      This may have the same root cause as JDK-8333766.

            vromero Vicente Arturo Romero Zaldivar
            acobbs Archie Cobbs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: