ClassName ref = new ClassName();
ref.setCredentials(Credentials);
ref.setVal(value);
ref.setUser(user);
Now when I create a new object of the same class reference, I still get the previous values I have set. Why is this so?
ClassName ref2 = new ClassName();
ref2.setVal(value);
ref2.setUser(user);
ref2.setSomethingNew(somethingNew);
My ref and ref2 instances have all the values [Credentials, Value, User and SomethingNew]. I want to differentiate these two instances. Is it because it’s holding the same object?
Update My Lapse:
It’s actually ref2 and not ref. I get the values in ref2 which i am not setting, and ref too holds a value which I am setting in the instance of ref2. Both are in same context.
Note that with
ref2you are only creating the object, but you are setting the values toref. You need:Note the
ref2change instead ofref.If your
ClassNameis overriding theequalsmethod, and all the inner objects are equal also, it is normal to have equality betweenrefandref2. You can useObject.equalsimplementation to detect if the objects are different (notedifferentvs.equal).