I have a below Java Class.
public class MyClass {
private final String id;
public MyClass(final String id) {
this.id= id;
}
public String getId() {
return id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final MyClass other = (MyClass) obj;
if (id== null) {
if (other.getId != null)
return false;
} else if (!id.equals(other.getId))
return false;
return true;
}
}
Now I am creating 2 objects of this class.
MyClass obj1 = new Object("ABCD");
MyClass obj2 = new Object("ABCD");
As per my class definition these 2 objects are equal. But in heap I believe 2 different objects would be created.
If I want to prove although both the objects are equal but still they are different how can I do it?