How does a method “reference to an instance method of an arbitrary object of a particular type” resolve the arbitrary object? [duplicate]

最后都变了- 提交于 2019-11-30 15:39:42

No, your understanding is correct. The documentation you linked implies (but does not adequately emphasize) that given a functional interface that expects args a1, a2, a3, ..., a method reference of this type is equivalent to a lambda that calls a1.namedMethod(a2, a3, ...).

Note that a concrete definition like this is required for consistency's sake - given the example on the linked documentation of a functional interface with two String arguments (String s1, String s2), how would you determine whether the behavior would be s1.doThing(s2) or s2.doThing(s1) otherwise?

You can find this specified precisely in the JLS:

If the compile-time declaration is an instance method, then the arguments to the method invocation expression (if any) are the second and subsequent formal parameters of the invocation method. Otherwise, the arguments to the method invocation expression are the formal parameters of the invocation method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!