title: Setting instance variables in a loop
body: <p>I am trying to make minesweeper in Java and I created a Box class, with an instance variable isMine which determines whether or not that box is a mine. I use a setter to set this variable, although the problem exists when I use the constructor instead. In my main method when I create and fill the ArrayList containing ArrayLists of Boxes, I also use a random number generator to set the isMine variables. I do this all inside the same loop I use to fill the ArrayLists.</p>
  
  <pre><code>    ArrayList&lt;ArrayList&lt;Box&gt;&gt; boxList = new ArrayList&lt;ArrayList&lt;Box&gt;&gt;();
      for(int i=0; i&lt;10; i++){
          boxList.add(new ArrayList&lt;Box&gt;());
      }
      for(int i=0; i&lt;boxList.size(); i++){
          for(int j=0; j&lt;10; j++){
              double rand = Math.random();
              boxList.get(i).add(new Box());
              boxList.get(i).get(j).drawBox(i,j);
              rand = Math.random();
              if(rand&gt;.2){
                  boxList.get(i).get(j).setMine(false);
              }
              else{
                  boxList.get(i).get(j).setMine(true);
              }
          }
      }
  </code></pre>
  
  <p>However it seems that every Box's isMine is actually set to whatever the last Box's isMine was set to. So if boxList.get(9).get(9).getMine() is false, they will ALL be false.</p>