1

Why is an instance needed for field whyIsAnInstanceRequired and won't work as in field errWhyIsAnInstanceRequired?

The compile error is:

Error: java: incompatible types: invalid method reference
            unexpected instance method function(programming.java.FunctionalQuestion.Helper,programming.java.FunctionalQuestion) found in unbound lookup

See code:

public class FunctionalQuestion {

    class Helper { }

    public void setValue (long value) { }

    ObjLongConsumer<FunctionalQuestion> whyIsAnInstanceNotRequired =
        FunctionalQuestion::setValue;

    public void function(Helper helper, FunctionalQuestion functionalQuestion) { }

    BiConsumer<Helper, FunctionalQuestion> whyIsAnInstanceRequired =
        new FunctionalQuestion()::function;

    /*
    * Error: java: incompatible types: invalid method reference
        unexpected instance method function(programming.java.FunctionalQuestion.Helper,programming.java.FunctionalQuestion) found in unbound lookup
    * */
    BiConsumer<Helper, FunctionalQuestion> errWhyIsAnInstanceRequired =
        FunctionalQuestion::function;

}
5
  • 2
    Well, function(…) is not static, hence requires an instance for an invocation, whether you invoke it directly like someInstance.function(…) or from a method reference.
    – Holger
    Jun 6, 2019 at 15:28
  • But on field whyIsAnInstanceNotRequired, it’s not in a static context and yet it seems to reference statically(without an instance) FunctionalQuestion::setValue, member function. Jun 6, 2019 at 16:22
  • 2
    If you want to bind the non-static context, i.e. this, just use this::function. Compare with ordinary method invocations; you can’t use FunctionalQuestion.function(…) regardless of the context. But you can use this.function(…) in a non-static context. Or otherObject.function(…) in any context. Omitting the context, like with function(…), is not supported for method references. If you want to have that, you’d need a lambda expression, (h,q) -> function(h,q).
    – Holger
    Jun 6, 2019 at 16:30
  • I think I understand the constraints about the static context. But how does it explain, why this works?: java.util.function.ObjLongConsumer<FunctionalQuestion> whyIsAnInstanceNotRequired = FunctionalQuestion::setValue; public void function(Helper helper, FunctionalQuestion functionalQuestion) {} vs java.util.function.ObjLongConsumer<FunctionalQuestion> whyIsAnInstanceNotRequired = this::setValue; private void setValue(FunctionalQuestion functionalQuestion, long l) { } Jun 6, 2019 at 22:41
  • I'm starting to realize it comes down to method signatures. Somehow this may imply the first argument, where static doesn't affect the method signature. Jun 6, 2019 at 22:45

1 Answer 1

1

I believe this explains why I was surprised by the original code.

This concept is called:

Reference to an instance method of an arbitrary object of a particular type.

Example: ContainingType::methodName as explained in: methodreferences.html

From that document:

The following is an example of a reference to an instance method of an arbitrary object of a particular type:

String[] stringArray 
       = { "Barbara", "James", "Mary", "John","Patricia", "Robert", "Michael", "Linda" };
Arrays.sort(stringArray, String::compareToIgnoreCase);

The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.compareToIgnoreCase(b).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.