Thursday 17 September 2009

Spot the deliberate mistake

Got caught out today doing some Jave code at work.

I thought I'd be a good boy and use Commons Lang utilities to implement toString(), hashCode() and equals(). Spot the mistake here:

@Override
public int hashCode() {
return new HashCodeBuilder(13, 73).append(field1).append(field2).hashCode();
}


Any ideas? Here's the corrected version:

@Override
public int hashCode() {
return new HashCodeBuilder(13, 73).append(field1).append(field2).toHashCode();
}


An easy mistake to make when using an IDE with auto-complete.

Luckily I was saved by my unit tests!