why Java 8 :: operator not working for Object hashcode method when we use super?

  • A+

Below code working

Function fun1= super::equals Function fun2= Object::hashCode  

But below code is not

Function fun1= Object::equals Function fun2= super::hashCode  

What is the concept here even though all are instance methods in Object class.

I have read somewhere the principals are:

  1. object::instance methods
  2. Class::static method
  3. Class:: instance Method


The Function interface is a functional interfaces representing a function that accepts one parameter and returns a value.

super::equals works because it is a function that accepts one parameter and returns a value.

Object::equals does not work because it is a function that accepts two parameters and returns a value.

If you have a method reference of the form ClassName::instanceMethod, the function that it represents will have an extra parameter - the object on which to call the method. According to the docs:

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)

super::equals is of the form instance::instanceMethod so there is no problem and works as you expect.

The correct functional interface types to use for Object::equals and super::hashCode is BiFunction<Object, Object, Boolean> and IntSupplier.

Also, don't use raw types.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: