I have a class with some non primitive members.
class Relation {
String name;
Role roleFrom;
Role roleTo;
}
class Role {
RoleType roleType;
String details;
}
class RoleType {
String typeName;
String details;
}
Two relations are equal, when
- the name are equal
- the role type (identified by unique typeName) are equal for the Role members (roleFrom and roleTo)
How to write equals and hashCode for class Relation. When tried with Netbeans, it only displays 3 fields (name, roleFrom and roleTo). Is it because, one should not access the primitive types in roleFrom and roleTo (roleType -> typeName). Or, please show an implementation.
thanks.
When implementing
hashCode()andequals()with non-primitive types of fields, it is assumed that these types also implementhashCode()andequals()correctly. So go to the other classes and implementhashCode()andequals()there (again using the auto-generation features of your IDE).