title: Java: Recursion and Passing by value
body: <p>I'm a bit confused regarding recursion in Java:</p>
  
  <pre><code>public int recursion(Board board2,int x){
      Board board1 = board2;
      if(x==3){
          System.out.println("End");
          return 0;
      }
  
      board1.fix(obj, Color.BLUE, x, 12);
      return recursion(board1, x+1);
  
  }
  </code></pre>
  
  <p>This is just an example of putting an object inside a board. How can I make changes to <code>board1</code>, but not to the original board that I passed in as a parameter? Since outside the method, the <code>Board</code> that I passed as parameter is changed after the execution, and I don't understand why. I was reading that Java passes by value and not by reference. I want to make the program return to its previous state during the recursion and not change the real board.</p>
  
  <p>EDIT</p>
  
  <p>I tried it this way, but it still doesn't work:</p>
  
  <pre><code>public int recursion(Board board2,int x){
      try{
          board1 = (Board)board2.clone();
      }catch (Exception e){
          System.out.println("Error "+e);
      }
      if(x==3){
          System.out.println("End");
          return 0;
      }
  
      board1.fix(obj, Color.BLUE, x, 12);
      return recursion(board1, x+1);    
  }
  </code></pre>
  
  <p>EDIT 2</p>
  
  <p>Here's how I implemented the clone method</p>
  
  <pre><code>class Board extends JPanel implements Cloneable{
      // ...
      @Override
      protected Board clone() throws CloneNotSupportedException{    
         return(Board) super.clone();  
      } 
      // ...
  </code></pre>
  
  <p>EDIT 3</p>
  
  <p>I still can't get it to work</p>
  
  <pre><code>class Board extends JPanel implements Cloneable{
      public Color[][] board; 
  
      @Override
      public Board clone(){ 
          Board board1 = new Board();
          board1.board = this.board;
          return board1;  
      }
      // ...
  }   
  </code></pre>