3 Replies - 346 Views - Last Post: 08 May 2011 - 09:35 AM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Overriding hashCode

Posted 08 May 2011 - 08:08 AM

I was wondering whether it would be correct (I know its not preferable) to override the hashCode method in this way:


public int hashCode()
{
    return 250;
}




I know that in this case every Object of that class would have same hashCode but that would not mean that they are equal.
Opinions. Is it a "work-able" implementation?
Is This A Good Question/Topic? 0
  • +

Replies To: Overriding hashCode

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2832
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Overriding hashCode

Posted 08 May 2011 - 08:25 AM

No - you'd have to have a very good reason to have every instance of a class returning the same hashcode. I can't think of one
Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: Overriding hashCode

Posted 08 May 2011 - 09:17 AM

The hashCode() method should return the same result for Objects that are equal as defined by the equals() method. If two Objects aren't equal, then the hashCode() method shouldn't return the same result for those two Objects.
Was This Post Helpful? 1
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Overriding hashCode

Posted 08 May 2011 - 09:35 AM

Ok.Thanks for replies. So i can sum up saying that its not correct to implement it that way:). Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1