title: for loop value or reference
body: <p>I'm having some trouble understanding the third loop in my code below. After the second loop the list didn't change so I assumed that d was a copy of the element in the list. After the third output the element has its name changed so it's not a copy.</p>
  
  <pre><code>public class Test {
      public static void main(String[] args) {
          List&lt;Dog&gt; list = new ArrayList&lt;&gt;();
  
          list.add(new Dog("a"));
          list.add(new Dog("b"));
          list.add(new Dog("c"));
          list.forEach(d -&gt; System.out.println("dog name: " + d.getName())); // a,b,c
  
          for (int i = 0; i &lt; list.size(); i++) {
              list.set(i, new Dog("z"));
          }
          list.forEach(d -&gt; System.out.println("dog name: " + d.getName())); // z,z,z
          for (Dog d : list) {
              d = new Dog("y");
          }
          list.forEach(d -&gt; System.out.println("dog name: " + d.getName())); // z,z,z
  
          for (Dog d : list) {
              d.setName("w");
          }
          list.forEach(d -&gt; System.out.println("dog name: " + d.getName())); // w,w,w
  
      }
  
      public static class Dog {
          private String name;
  
          public Dog(String n) {
              name = n;
          }
  
          public String getName() {
              return name;
          }
  
          public void setName(String name) {
              this.name = name;
          }
      }
  }
  </code></pre>