Map.Entry.setValue as a default method

John Rose john.r.rose at
Thu Nov 21 01:08:50 UTC 2013

On Nov 20, 2013, at 4:31 PM, Remi Forax <forax at> wrote:

> But while you can declare a default hashCode and equals, it will not work because the implementation of Object.hashCode and Object.equals will always be preferred to the default methods by the VM, this is how default methods are specified. Not something I'm very proud.

Next question:  What's the best practice for declaring reusable code for exactly those restricted methods (hashCode/equals, toString)?  Because of the irregularity with Object, the opt-in isn't by default, but there should still be a convention for supplying the code as a "would-be default method".

Maybe one of:
  interface KoolReusablePair {
    default boolean defaultEquals(Object x) { ... }
    static int hashCode(KoolReusablePair self) { ... }

  class KoolImpl implements KoolReusablePair {
    @Override //manual opt-in:
    public boolean equals(Object x) { return KoolReusablePair.super.defaultEquals(x); }
    @Override //manual opt-in:
    public int hashCode() { return KoolReusablePair.hashCode(this); }

— John

More information about the core-libs-dev mailing list