title: Adding an object to an ArrayList from a different class (Chess in Java)
body: <p>I am writing a Chess program in Java. In my Board class, which deals with all movements of the Piece, I created a function called movePiece that moves a piece to a specific location and captures the enemy block if it exists on that location. The function for the movePiece looks like this:</p>
  
  <pre><code>public void movePiece(int x, int y, Piece pieceToMove) {
  
      if(pieceToMove.canMove(board, this.x, this.y, x, y)){ //Check if the piece canMove to the location
  
          if(board[x][y] == null) { //Check if the location is empty
  
              removePiece(this.x, this.y);
              board[x][y] = pieceToMove;
  
          } else {
  
              if(board[x][y].getColor() != pieceToMove.getColor()) { //Check if the location is occupied by an enemy
  
                  removePiece(this.x, this.y);
                  board[x][y] = pieceToMove;  
  
              } else {
                  System.out.println("Invalid Move");
              }
          }
  
      } else {
  
          System.out.println("Invalid Move");
  
      }
  }
  </code></pre>
  
  <p>It works fine, but I wanted to keep in track of the captured enemy Piece in an ArrayList. So, I created an ArrayList capturedList inside a Player class to keep in track of it, and tried to add </p>
  
  <pre><code>        if(board[x][y].getColor() != pieceToMove.getColor()) { //Check if the location is occupied by an enemy
  
              addCapturedPiece(board[x][y], capturedList); &lt;--- THIS LINE
              removePiece(this.x, this.y);
              board[x][y] = pieceToMove;  
  
          }
  </code></pre>
  
  <p>But this did not work because capturedList is inside the Player class. Is there a way to make this work smoothly?</p>