Wednesday 15 April 2015

java - Is it bad practice to equal() class instances by just their unique instanceID? -


I have a class that, at the instant, creates a unique ID for its object like:

  Public square Fu {Private Final Static Atomic Angle UNIQUE_ID = New Atomicator (0); Private fin IND; Name of private final string; Private finals IH Hashod; Public Fu (string name) {id = UNIQUE_ID.GetAndIncrement (); This.name = name; Int result = 17; Int result = 31 * result + ID; Int result = 31 * result + name.hashCode (); Hashod = Result; } Public int getInstanceID () {return ID; }  

Now I want to apply the hashod and is equal to:

  public boolean par (object obj) {if (obj == This); If (Obj instanceof Foo) return false; Fu other = (Foo) Ob; Return Other GetInstanceID == getInstanceID (); } Public Action Hashod () {Return Hashod; }}  

Am I considered bad behavior if I can compare the ID of the horrors of another area, which can vary from one Fu Isense to another? Also should I still use every field in the hashawood function or ID?

Good contract happens in this way - If two objects are the same then they have one Must be Hashod . You have not override the hashhead () , so you can have 2 identical objects with different hashodes that are wrong. To comply with the contract, you can override hashhead () , then it may be OK .


No comments:

Post a Comment