A huge hint: remove the static modifier from the Dog's dogName field and you will get what you expect. A general word of advice: Be careful with the static modifier
Your Dog class should be like this:
class Dog{
private String dogName;
Dog(String name){
dogName=name;
}
public void setName(String newName){
dogName=newName;
}
public String getName(){
return dogName;
}
}
As for your "pass-by-value" experiment, may I suggest that rather than printing the dogName, just print the object reference. In other words modify your test like this:
class JavaIsPassByValue{
public static void main(String arr[]){
Dog dog1=new Dog("OldDog");
new JavaIsPassByValue().display(dog1);
System.out.println("dog1: " + dog1);
}
public void display(Dog d){
System.out.println("d in moment 1: " + d.getName());
d = new Dog("NewDog");
System.out.println("d in moment 2: " + d.getName());
}
}