Skip to Content
Java Projects - Second Edition
book

Java Projects - Second Edition

by Peter Verhas
August 2018
Intermediate to advanced
524 pages
14h 45m
English
Packt Publishing
Content preview from Java Projects - Second Edition

Implementing equals and hashCode

Implementing these methods is fairly simple. As this is a very common task, the IDEs support the generation of these methods. These methods are tied together so much that the menu items in the IDEs are not separate; they allow you to generate these methods at once.

Asking the IDE to generate the equals() method will result in something like the following code:

@Override public boolean equals(Object o) {   if (this == o) return true;   if (o == null || getClass() != o.getClass()) return false;     MyObjectJava7 that = (MyObjectJava7) o;  return Objects.equals(field1, that.field1) &&   Objects.equals(field2, that.field2) &&   Objects.equals(field3, that.field3); }

For this sample, we have three Object fields named ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java 11 Cookbook - Second Edition

Java 11 Cookbook - Second Edition

Nick Samoylov, Mohamed Sanaulla

Publisher Resources

ISBN: 9781789131895Supplemental Content