Search code examples
javaclasscomparisonequalshashcode

Should I always override equals, hashcode and toString methods?


I have rather simple question: According to best programming practices, when I create a new model class should I always override equals, hashcode and toString methods? Even I do not intend to compare objects of specific class?


Solution

  • Each of these methods has its own significance. equals and hashCode methods are used for comparison and hashing and toString mainly for logging purposes. If you don't want any of these functionalities, you are not required to implement them.