title: How do I solve local referenced variables inside a for loop?
body: <p>I'm writing a <a href="https://fr.wikipedia.org/wiki/Swing_(Java)" rel="nofollow noreferrer">Swing</a> application and trying to make a menu where each <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/JMenuItem.html" rel="nofollow noreferrer">menu item</a> has its own <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/Action.html" rel="nofollow noreferrer">action</a>: </p>
  
  <p>Here's how I wanted to solve this: </p>
  
  <pre><code>private void createGameLevelMenuItems(JMenu menu){
      for (int i = 0; i&lt;10; i++) {
          JMenuItem item = new JMenuItem(new AbstractAction("Level-" + i) {
              @Override
              public void actionPerformed(ActionEvent e) {
                  game.loadGame(i);
                  board.refresh();
                  pack();
              }
          });
          menu.add(item);
      }
  }
  </code></pre>
  
  <p>However, I cannot use <code>loadGame(i)</code>, because it says <code>i</code> would have to be final. I understand the reason for this, but I do not know how to work my way around it.</p>