ProLesson 4.8Intermediate3 min readUpdated 30 October 2026
final, and the equals, hashCode and toString methods
Use final correctly, then override toString, equals and hashCode the right way, and learn the contract that keeps HashMap and HashSet working.
Every class inherits from Object, and three of its methods matter in almost every project: toString, equals and hashCode. Getting equals and hashCode wrong causes some of the most confusing bugs in Java. First, a quick look at final.
01The three uses of final
java
final int maxRetries = 3; // variable: can't be reassigned
final class PriceCalculator { } // class: can't be extended (String is final)
final void audit() { } // method: can't be overriddenLoading the rest of the lesson…