java - How does a method "reference to an instance method of an arbitrary object of a particular type" resolve the arbitrary object? -


this question has answer here:

the oracle java 8 documentation defines 4 types of method references can use instead of lambda expressions. trying understand kind of method reference described as: "reference instance method of arbitrary object of particular type " written containingtype::methodname.

i not sure if missing something, me seems more like: "reference first parameter of abstract method of functional interface, assuming of type containingtype". tried come examples 'arbitrary object' second parameter, of course not compile.

is there official reference how object resolved compiler? correct in understanding that:

  1. the arbitrary object has 1st parameter of abstract method of functional interface.
  2. the signature of method reference must same of abstract method of functional interface, without first parameter.

so functional interface abstract method a method(b b, c c, d d) can passed instance method references x::methodimpl or b::methodimpl. there no way can pass c::methodimpl example, instance of class c signature a methodimpl(b b, d d).

are there other cases missing, might reason why oracle wrote in such ambiguous way?

no, understanding correct. documentation linked implies (but not adequately emphasize) given functional interface expects args a1, a2, a3, ..., method reference of type equivalent lambda calls a1.namedmethod(a2, a3, ...).

note concrete definition required consistency's sake - given example on linked documentation of functional interface 2 string arguments (string s1, string s2), how determine whether behavior s1.dothing(s2) or s2.dothing(s1) otherwise?

you can find specified precisely in the jls:

if compile-time declaration instance method, arguments method invocation expression (if any) second , subsequent formal parameters of invocation method. otherwise, arguments method invocation expression formal parameters of invocation method.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -