How to explain shallow-copy in Java when it's suppose to be pass by value?
<p>From what I understand with Java, everything is pass by value. However, when you begin talking about objects which contain other objects things get a bit interesting and i'd like to be able to communicate this better and make sure I fully understand as well. So my take here is that only the primitives within the object are actually copied, on the other hand any objects it contains...the values of those objects are not copied, rather only are copies of those references.</p>

<p>So if we had some object with these members</p>

<pre><code>public class SomeObject {
   private String name;
   private String address;
   private  List&lt;String&gt; friends;

   public SomeObject(String name, String address, List&lt;String&gt; friends) {
      this.name = name;
      this.address = address;
      this.friends = friends;
   }
}
</code></pre>

<p>And we did this:</p>

<pre><code>List&lt;String&gt; friendsList = new ArrayList&lt;String&gt;();
        friendsList.add("bob");
        friendsList.add("joe");

        SomeObject so1 = new SomeObject("mike", "123 street", friendsList);

        friendsList.add("zoe");
        friendsList.add("rick");
        SomeObject so2 = new SomeObject("david", "300 ave", friendsList);

        System.out.println(so1.getName() + " " + so1.getAddress());
        for (String friend : so1.getFriends()) {
            System.out.println(friend);
        }

        System.out.println(so2.getName() + " " + so2.getAddress());
        for (String friend : so2.getFriends()) {
            System.out.println(friend);
        }
</code></pre>

<p>Our output is this:</p>

<pre><code>mike 123 street
bob
joe
zoe
rick
david 300 ave
bob
joe
zoe
rick
</code></pre>

<p>Even though we have created 2 separate objects, they both maintain a reference to the original friendslist. This is why I get a bit tripped up when people say Java is always pass by value. </p>
