I am struggling with the reason why I am getting the second line of outputs as "Line2: null" when running the following piece of code on HashMap:
import java.util.*;
class Dog {
  public Dog(String n) {name = n;}
  public String name;
  public boolean equals(Object o) {
    if((o instanceof Dog) && (((Dog)o).name == name)) {
        return true;
    }
    else {return false;}
  }
  public int hashCode() {return name.length();}
}
public class HelloWorld{
 public static void main(String []args){
    Map<Object, Object> m = new HashMap<Object, Object>();
    Dog d1 = new Dog("clover");
    m.put(d1, "Dog key");
    System.out.println("Line1: " + m.get(d1));
    d1.name = "magnolia";
    System.out.println("Line2: " + m.get(d1));
    d1.name = "clover";
    System.out.println("Line3: " + m.get(new Dog("clover")));
    d1.name = "arthur";
    System.out.println("Line4: " + m.get(new Dog("clover")));
 }
}
The outputs displayed are:
Line1: Dog key
Line2: null
Line3: Dog key
Line4: null
Yes, I do realize that modifying the instance variable name, in turn, affects the hashcode of the instance of Dog because of the way I calculate the hashcode. But, I am using the same instance as the key! So, why cannot the get() method find the corresponding value? It seems like once a pair is pushed into a HashMap, the key is hardcoded with the value forever! Is this how it is supposed to work, meaning that, once a hashcode has been determined for a value before placing the pair in HashMap, the hashcode can never be modified again?
 
    