title: Java: methods appears to be passing by reference
body: <p>I have always understood Java to pass parameters by value. I have some code that I cannot seem to debug. Here's a simplified version:</p>
  
  <pre class="lang-java prettyprint-override"><code>private isFinished = false;
  private int target, count;
  
  public Example(int target){
      this.target = target;
  }
  public void doProcess(int x){
      count += x;
  }
  public boolean isFinished(){
      if(x &lt; target){
          return false;
      } else {
          return true;
      }
  }
  
  
  private Example example;
  
  public Test(Example e){
      this.example = e;
  }
  public isFinished(){
      return e.isFinished();
  }
  public void doProcess(){
      e.doProcess(3);
  }
  
  
  private Example example;
  public Generate(Example e){
      this.example = e;
  }
  public void generate(int num){
      for(int y=0; y &lt; num; y++){
          Test t = new Test(example);
          while(t.isFinished == false){
              t.doProcess();
          }
      }
  }
  </code></pre>
  
  <p>The Generate class takes an Example as the argument. It uses this Example and passes it to "Test". What's happening is that when Generate.generate() is called, the first iteration works properly, but at each iteration a new Test is supposed to be made with the example being passed as a parameter. The 'example' appears to be getting altered when doProcess() is called, when what I want is for a fresh Test to be created each time using the same Example that was passed to Generate when it was created.</p>