As has been stated by many answers "Rover" will be printed because you have changed the actual string inside dog
. You haven't changed the String itself (which you can't do as Strings are immutable, aka unchangeable).
public static void main(String args[])
{
Dog myDog = new Dog("Rover"); //myDog contains STRING1 ("Rover")
String dogName = myDog.getName(); //dogName is set to refer to STRING1 ("Rover")
myDog.setName("Max"); //myDog name set to STRING2 ("Max") (STRING1 unaffected)
System.out.println(dogName); //dogName still refers to STRING1 ("Rover"), "rover" printed
}
More general concept
This may give the impression that once you retrieve an internal object from another object using a getter it is entirely separate and you can change it as you see fit. This isn't always the case. Strings are immutable so you can't change their internal state. But if you accessed an object that was mutable (aka could be changed) and changed its internal state outside the Dog
object then it would still affect the internal state of the Dog
object. I will use an ArrayList
to show this concept
public class Dog {
public ArrayList<String> names=new ArrayList<String>();
public ArrayList<String> getNames() {
return names;
}
public void setNames(ArrayList<String> names) {
this.names = names;
}
public static void main(String[] args){
Dog dog=new Dog();
dog.getNames().add("Rover");
ArrayList<String> someArrayList=dog.getNames();
someArrayList.add("Rex");
System.out.println(dog.getNames().contains("Rex")); //prints true. dog's internal state effected from afar
someArrayList=new ArrayList<String>(); //change someArrayList to refer to a whole new ArrayList
someArrayList.add("bad");
System.out.println(dog.getNames().contains("bad")); //prints false, someArrayList now points to a different ArrayList to the one internal to dog
}
}
As you can see if you retrieve the arrayList and then add objects to it this would affect the ArrayList within Dog. If you retrieve the arrayList and set your variable to a completely different ArrayList then this won't affect the arrayList within Dog (because its a different ArrayList). Think of objects as being "no where in particular" and variables being addressed to those objects.
So an analogy; you (a Dog
object) have an address book of houses you care about (other objects). You can change your address book to point to a different house, no one else will care about that. But if you post a letter to a house saying "paint the door green" everyone else with an address to that house will notice.
The general rule is if you change a variable to point to a different object only that variable is changed (like changing the address in your address book), this always involves an =
and may involve a new
(but may not and the new is hidden with strings) . On the other hand if you use a method on an object (like sending a parcel to a house to which you have an address) then everywhere that object is visible will see the change (this always involves a .
to a method that is a setter (or otherwise changes the object, like add
))