Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9107095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:29:27+00:00 2026-06-17T02:29:27+00:00

When overriding the equals() and hashCode() methods of a class, would the following approach

  • 0

When overriding the equals() and hashCode() methods of a class, would the following approach work?

All my logic to see if the objects are equal is done in my equals() method. The class has a static variable (we’ll call it hashCodeReturn) that is set to 1.

When ever the equals method reaches logic that would return false, it adds 1 to hashCodeReturn

The hashCode() then simply returns the hashCodeReturn value

Is there any reason this would not work?

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:29:29+00:00Added an answer on June 17, 2026 at 2:29 am

    No, it would not work.

    The hash code of two objects that are the same (e.g. the same reference) should be the same. By returning the result of a static variable you’ll break this contract.

    The general contract of hashCode is:

    Whenever it is invoked on the same object more than once during an
    execution of a Java application, the hashCode method must consistently
    return the same integer, provided no information used in equals
    comparisons on the object is modified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class
Possible Duplicate: Overriding equals and hashCode in Java If I have class A {
What issues / pitfalls must be considered when overriding equals and hashCode ?
It looks like hashCode() and equals() are declared as final. So overriding the implementation
Possible Duplicate: Returning false from Equals methods without Overriding One of my colleague asked
In the post What issues should be considered when overriding equals and hashCode in
I'd like to use Apache commons HashCodeBuilder and EqualsBuilder for overriding hashCode() and equals()
I have a set of objects which I cannot modify by overriding their equals
Is there a specific rule on how Overriding equals() & hashCode() in sub classes
I am pasting my code below; My base class is overriding Equals and getHashcode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.