• Post Reply Bookmark Topic Watch Topic
  • New Topic

a weird behaviour by using a Method Reference in eclipse

 
Pierrot Mongonnam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello together,

I was about to solve the exercise from the following link: Exercise 2d from the Oracle's Java Tutorials. Following my Code:


From line 38 to 56 my implemented methods as answers to points a,b,c,d followed by an additional method implementation on my own at line 63. But the compiler is complaining at line 128 while calling the method print(Function(Integer, Boolean) at line 48 with the 2 following error messages:The method print(DataStructure.DataStructureIterator) in the type DataStructure is not applicable for the arguments (DataStructure::isEvenIndex)
The target type of this expression must be a functional interface

?!?! I mean I'm about to call the print method at Line 48 with a method-reference!!! Could some body tell me what the hell is going on here?!?! What I'm doing wrong?? I've checked the solution from Oracle here DataStructure.java. I don't see any big difference and it works.
 
Carey Brown
Bartender
Posts: 2236
27
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had to change to this

Made static and returned boolean.
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you don't need ternary operator here.. you could use:

 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:you don't need ternary operator here.. you could use:



... exactly like the isOddIndex() method in the original post.
 
Pierrot Mongonnam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all you guys. It was very silly from me to have just forgotten that this Method-Reference call (DataStructure::isEvenIndex) is for static methods.

Tushar Goel , Paul Clapham
I was so desesperate that I changed the implementation of the function in that way as I checked the Oracle solution. I thought the error was in my argument and my returned value e.g. you have to produce a Boolean. Such a silly thing

Thanks again you all!!! See you at the next non sense from me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!