Page 1 of 1
About Question enthuware.ocpjp.i.v11.2.3081 :
Posted: Fri Oct 25, 2019 2:02 pm
by DazedTurtle
Why does this compile? I would have thought it would have to be
Why aren't the parentheses needed?
Re: About Question enthuware.ocpjp.i.v11.2.3081 :
Posted: Fri Oct 25, 2019 10:37 pm
by admin
Student::debug is not an invocation of the method. It is a method reference. It is simply a shortcut for the programmer. The compiler converts it into an actual method call, something like this:
Code: Select all
new Consumer(){
public void consume(Student s){
s.debug();
}
}
You may want to read about method references from a book for more details.
Re: About Question enthuware.ocpjp.i.v11.2.3081 :
Posted: Sun Nov 24, 2019 9:45 am
by pere00
Hi,
At question 35 you say the lists forEach method requires a method with a parameter.
Nevertheless the method debug() does not take any parameter, why does this work, and example 35 dont?
Thanks.
Re: About Question enthuware.ocpjp.i.v11.2.3081 :
Posted: Sun Nov 24, 2019 10:10 am
by admin
Please read the post above. It explains exactly what you are asking about debug().
I am not sure which question 35 are you referring to. Please mention the questionid so that the question can be identified definitively.
Re: About Question enthuware.ocpjp.i.v11.2.3081 :
Posted: Sun Nov 24, 2019 12:00 pm
by pere00
Hi,
Question id enthuware.ocpjp.ii.v11.2.1789
Re: About Question enthuware.ocpjp.i.v11.2.3081 :
Posted: Sun Nov 24, 2019 4:42 pm
by pere00
Now it is clear, on the other example the forEach elements are Strings, and printNames is not static, that leads to a compile error.
Re: About Question enthuware.ocpjp.i.v11.2.3081 :
Posted: Sun Nov 24, 2019 10:40 pm
by admin
The forEach method expects a Consumer object (i.e. an object of a class that implements Consumer). Since, Consumer is a functional interface (i.e. it has exactly one abstract method, which takes Student as an argument, in this case), the compiler provides help in creating such as class if you tell which method you want to invoke when Consumer's functional method is invoked. Method reference such as s::debug() works because the compiler knows that it need to invoke the debug method on the student instance passed to the functional method (see code above).
In case of n.getList().forEach(Names::printNames); (of question enthuware.ocpjp.ii.v11.2.1789), the compiler is not able to figure out how to call the printNames method. Since printNames is not static, the compiler can't use Names.printNames() to invoke it on Names class, and since the list is of Strings (not Names), the compiler has a reference to a String, it doesn't have any reference to an instance of Names on which to invoke printNames() method. Thus there is no way for the compiler to create an appropriate implementation of the Consumer interface using Name::printNames method reference.
I will suggest you to go through a good book because it is not really possible to provide complete details on forum posts.
HTH,
Paul.