title: Method runs for all instances of a class
body: <p>I ran across this problem, which has been driving me nuts.  In a nutshell, I instantiate two objects of the same class.  When I run a method in one object, the other object is affected too as if I called a method on a 2nd object explicitly.  I was wondering if someone could please give me a hand on this.  </p>
  
  <p>Suppose, I have class <code>Portfolio</code>...</p>
  
  <pre><code>public class Portfolio implements Cloneable {
  
  public ArrayList&lt;Instrument&gt; portfolio;
  private String name;
  private String description;
  
  public Portfolio() {
      portfolio = new ArrayList&lt;Instrument&gt;();
  }
  
  public Portfolio(Portfolio copyFrom) {
      //attempt to copy the object by value
      this.portfolio = (ArrayList&lt;Instrument&gt;) copyFrom.portfolio.clone();
      this.name = copyFrom.name;
      this.description = copyFrom.description;
  }
  
  public void applyStress(Stress stress) {
      this.portfolio.set(0,this.portfolio.get(0)+1;
  }
  </code></pre>
  
  <p>1st constructor is used to instantiate an object, etc. 2nd constructor is used to copy an object by value.</p>
  
  <p>A method <code>applyStress</code> is used to run through sum calculations. in our case I simplified the method, so that it does nothing but adds +1 to whatever is in the object.</p>
  
  <p>So I would instantiate an object as </p>
  
  <pre><code>Portfolio p = new Portfolio();
  </code></pre>
  
  <p>then I would assign to a <code>portfolio</code> field, some instruments;</p>
  
  <pre><code>p.portfolio = someInstrumentList;
  </code></pre>
  
  <p>then I would copy by value the <code>portfolio p</code> into <code>pCopy</code>:</p>
  
  <pre><code>Portfolio pCopy = new Portfolio(p);
  </code></pre>
  
  <p>So at this time I am have two objects that are the same.  Further one is a copy-by-value object. Changing values of fields in <code>pCopy</code> does not affect same fields in <code>p</code>.  </p>
  
  <p>Now, when I run a method <code>applyStress</code> on <code>p</code>, then the values of the instrument list in <code>pCopy</code> will also change.</p>
  
  <p>In other words, if <code>p.portfolio.get(0) == 1</code>, then after <code>p.applyStress</code>, I would expect to see that <code>p.portfolio.get(0)</code> is <code>2</code> and <code>pCopy.portfolio.get(0)</code> is <code>1</code> </p>
  
  <p>But what I see instead is <code>p.portfolio.get(0)</code> is <code>2</code> and <code>pCopy.portfolio.get(0)</code> is also <code>2</code> </p>
  
  <p>I do not understand why this happens.  It is not the <code>static</code> modifier issue, as there is no static modifiers.  Anyone has any ideas?</p>