title: Java ArrayList Class Attribute Mutator and Java Object References
body: <p>Suppose a class has an ArrayList attribute:</p>
  
  <pre><code>private ArrayList&lt;Object&gt; objects;
  </code></pre>
  
  <p>Is it possible to have a setter / mutator method for this attribute, like so:</p>
  
  <pre><code>public void setObjects(ArrayList&lt;Object&gt; objects) {
       this.objects = objects;
  }
  </code></pre>
  
  <p>Or do you have to do something more complicated because of the way Java does things?</p>
  
  <p>I tried to to test this myself with the following two classes:</p>
  
  <p><strong>Test.java</strong></p>
  
  <pre><code>import java.util.*;
  
  public class Test {
  
      private ArrayList&lt;String&gt; strings;
  
      public Test() {
          strings = new ArrayList&lt;String&gt;();
      }
  
      public ArrayList&lt;String&gt; getStrings() {
          return this.strings;
      }
  
      public void setStrings(ArrayList&lt;String&gt; strings) {
          this.strings = strings;
      }
  }
  </code></pre>
  
  <p><strong>testTest.java</strong></p>
  
  <pre><code>import java.util.*;
  
  public class testTest {
      public static void main(String[] args) {
          ArrayList&lt;String&gt; strings = new ArrayList&lt;String&gt;();
  
          strings.add("One");
          strings.add("Two");
          strings.add("Three");
  
          Test test = new Test();
          test.setStrings(strings);
  
          System.out.println("First get() of index 0: "+test.getStrings().get(0));
          System.out.println("Modifying test's strings object...");
  
          test.getStrings().set(0, "1");
  
          System.out.println("First get() of index 0: "+test.getStrings().get(0));
          System.out.println("Change it back... using strings variable");
  
          test.setStrings(strings);
  
          System.out.println("First get() of index 0: "+test.getStrings().get(0));
      }
  }
  </code></pre>
  
  <p><strong>Output</strong>:</p>
  
  <p>First get() of index 0: One</p>
  
  <p>Modifying test's strings object...</p>
  
  <p>First get() of index 0: 1</p>
  
  <p>Change it back... using strings variable</p>
  
  <p>First get() of index 0: 1</p>
  
  <p>The last print statement should show that first index is "One" .</p>
  
  <p>So, any ideas how I can achieve my original intent?</p>