title: java static map doesn't update in other class
body: <p>Hi there and thanks for reading,</p>
  
  <p>I have a Map initialized as follows:</p>
  
  <pre><code>    static HashMap&lt;Point, Tile&gt; map = new HashMap&lt;Point, Tile&gt;();
  </code></pre>
  
  <p>a Tile is a small class that holds an image and a function called <code>addFloor(int tileCode)</code>:</p>
  
  <pre><code>class Tile {
  
      BufferedImage img;
      int imgHeight, imgWidth;
  
      public Tile(BufferedImage img) {
          this.img = img;
          imgHeight = img.getHeight();
          imgWidth = img.getWidth();
      }
  
      public Tile addFloor(int tileCode) {
          BufferedImage newImg = Helper.joinBufferedImageFloor(this.img, Map.buildings.get(tileCode).img);
          Tile newTile = new Tile(newImg);
          return newTile;
      }
  }
  </code></pre>
  
  <p>The <code>addFloor(int tileCode)</code> function just puts an image obtained by another map via the <code>tileCode</code> as key (Map is just another class that holds the map of Tiles and maps of asset images, and also some unimportant functions for now) on top of the Tile image (<code>this.img</code>) with an offset.</p>
  
  <p>(Are you still with me or do i need to give more info?)</p>
  
  <p>and now:</p>
  
  <pre><code>Tile tileDebug = Map.map.get(new Point(10, 10));
  Map.map.replace(new Point(10, 10), Map.map.get(new Point(10, 10)).addFloor(tileCode)); //I've also tried map.put()
  tileDebug = Map.map.get(new Point(10, 10)); //MARK 1
  panel.repaint();
  </code></pre>
  
  <p>I checked via eclipse debugger and I checked the <code>addFloor(int tileCode)</code> function seperated:</p>
  
  <p>-the <code>addFloor(int tileCode)</code> function works perfectly. It creates a new image and returns a completely new Tile with the correct image.</p>
  
  <p>-using the debugger I saw that the Tile updated, it changed its image and has another hash now (same as returned by <code>addFloor(int tileCode)</code>).</p>
  
  <p>-Currently the <code>paintComponent(Graphics g)</code> function has just one call to <code>drawMap(Graphics2D g2d)</code> which draws Tile by Tile.</p>
  
  <p>-Since the <code>map.replace()</code> call the map hasn't been edited.</p>
  
  <pre><code>private void drawMap(Graphics2D g2d) {
      if (map != null) {
          for (int y = 1; y &lt;= 100; y++) {
              for (int x = 1; x &lt;= 100; x++) {
                  Tile currentTile = Map.map.get(new Point(x, y)); //MARK 2
                  if (x == 10 &amp;&amp; y == 10) {
                      //This line is just so I can add a breakpoint to debug the tile currentTile
                      System.out.println("");
                  } //Unimportant from here, just positioning and drawing, that works perfectly
                  int dx = 350, dy = 0;
                  dx += Main.viewX;
                  dy += Main.viewY;
                  int posX, posY;
                  posX = (int) ((x * 64 + y * -64));
                  posY = (int) ((x * 32 + y *  32));
  
                  posX -= currentTile.imgWidth;
                  posY -= currentTile.imgHeight;
                  g2d.drawImage(currentTile.img, posX + dx, posY + dy, this);
  
              }
  
          }
      }
  }
  </code></pre>
  
  <p>The problem is that the drawn Tile isn't the old Tile, it isn't the Tile that got returned by <code>addFloor(int tileCode)</code>. The drawn Tile is a new one, with its own hash and image. The image that is drawn is the second image passed to <code>joinBufferedImageFloor(BufferedImage img1, BufferedImage img2)</code> (the one obtained via the <code>tileCode</code> in <code>addFloor(int tileCode)</code>.</p>
  
  <p>I followed my entire program from <code>//MARK 1</code> to <code>//MARK 2</code>, but the map never got explicitly edited but the return value of </p>
  
  <pre><code>Map.map.get(new Point(10, 10))
  </code></pre>
  
  <p>changed from the correct Tile (MARK 1) to the third Tile (MARK 2)</p>
  
  <p>Is it because the Map is static? (Note: Only 2 Threads explicitly edit the Map, the Main Thread and a KeyListener Thread, but in this use case this Thread wasn't involved)
  Whats wrong here?
  Am I getting something wrong about Java Maps?</p>
  
  <p>Any help appreciated thanks in advance</p>
  
  <p>Edit 1min after posting it:
  I think I described it a bit confusing here and there so please ask any question that might be unanswered in this description.
  On wish I can also provide you with the full Project setup etc...</p>
  
  <p>Edit few Hours after:
  Edited the Question according to the how-to-ask section of StackOverflow</p>
  
  <p>Edit for anyone interested the <code>joinBufferedImageFloor(BufferdImage img1, BufferedImage img2)</code> function:</p>
  
  <pre><code>public static BufferedImage joinBufferedImageFloor(BufferedImage img1, BufferedImage img2) {
          int offsetX = 16;
          int offsetY = -32;
          int width = img1.getWidth();
          int height = img1.getHeight() - offsetY;
          BufferedImage newImage = new BufferedImage(width, height,
              BufferedImage.TYPE_INT_ARGB);
          Graphics2D g2 = newImage.createGraphics();
          Color oldColor = g2.getColor();
          g2.setColor(Color.BLACK);
          g2.fillRect(0, 0, width, height);
          g2.setColor(oldColor);
          g2.drawImage(img1, null, 0, -offsetY);
          g2.drawImage(img2, null, offsetX, 0);
          g2.dispose();
          return newImage;
  }
  </code></pre>