Eksempel

public InstanceofEquals_Example( String str ) {
super();
this .str = str;
}

public String getString(){
return str;
}

public int hashCode(){
return str.hashCode();
}

public boolean equals( Object other ) {
return str.equals( ( (InstanceofEquals_Example)other ).getString() );
}

private String str;

Løsning
Bruk instanceof i equals() før casting.

public InstanceofEquals_Solution( String str ) {
super();
this .str = str;
}

public String getString(){
return str;
}

public int hashCode(){
return str.hashCode();
}

public boolean equals( Object other ) {
return ( other instanceof InstanceofEquals_Solution) &&
str.equals( ( (InstanceofEquals_Solution)other ).getString() );
}

private String str;