Methods Common To All Objects

Java Best Practices Series

Methods Common To All Objects

10. Follow The Guidelines Of The Equals-Method

The equals-method need to obey the following four rules to remain functional.

Reflexive: x.equals(x) = true

Symmertric: x.equals(y) = y.equals(x)

Transitive: x.equals(y) = y.equals(z) = true -> z.equals(x) = true

Consistent: x.equals(y)  remains the same as long as x and y does not change.

How to write a good equals method:

(Regardless of most IDEs autogenerating them)

  • First use == to check if the object is equivilent to “this”.
  • Use instanceof to check if the object is the correct class instance. Otherwise return false.
  • Cast the object to the same instance as “this”
  • Check with == if all relevant fields of the class match each other.

11. Always Override Hashcode When Overriding Equals

Given that the hashcode-method is based on the equals-method, it is important to override it after the equals-methods have been overriden.

Otherwise all uses of hashmaps and other hashcode reliant functionalities will become useless.

12. Override ToString With Valuable Instance Information

If the toString-method is not override, it will always write a valueless object-instance reference.

To avoid irrelevant information being printed out (e.g. to logs), it is recommended to override the method and provide relevant object-instance information.

13. Be Cautious When Overriding The Clone-Method

The clone-method is a protected method declared in the original Object.class. However, in order to use it, a class must implement the Cloneable-interface and afterwards override the clone-method.

Within this overridden method, one must call super.clone and return the casted result.

Additionally, two things need to be considered as well:

  1. If the class extends any other classes before Object.class, these superclasses also needs to override the clone-method.
  2. If the object has any relevant fields, which also needs to be cloned, the classes of these fields also needs to override clone and be assigned to the originally cloned object.

Since the cloned fields need to be assigned to the clone object, the clone method is not compatible with immutable objects.

14. Implement The Comparable-Interface On Data Objects

The Comparable-interface allows you to implement the compareTo-method, which can be used to declare the rules for how to compare one instance of a class to another instance of the same class.

The compareTo-method returns an int (Integer) value, which can either be a positve, negative or zero, dependent on the result of the comparison.

Having an implementation of the Comparable-interface on DTOs or other types of data containing objects, allows for quick and easy sorting and arrangement of the various instance within lists or arrays.

Recommended Reading

Article

  • Obey the general contract when overriding equals – The Finest Artist– Blog

Books

Video Tutorial 

    • Effective Java for Android Developers – Item #11 – Fragmented – YouTube
    • Comparable Interface (Java) – Nathan Schutz – YouTube

About

Hi, I'm the Author

My name is Daniel H. Jacobsen and I’m a dedicated and highly motivated software developer with a masters engineering degree within the field of ICT. 

I have through many years of constantly learning and adapting to new challenges, gained a well-rounded understanding of what it takes to stay up to date with new technologies, tools and utilities. 

The purpose of this blog is to share both my learnings and knowledge with other likeminded developers as well as illustrating how these topics can be taught in a different and alternative manner.

If you like the idea of that, I would encourage you to sign up for the newsletter.

Cheers! 🍺

Didn't Find What You Were Looking For?

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Scroll to Top