title: Edit a local variable inside java lambda
body: <p>I need to change a local variable inside a lambda event handler in <a href="/questions/tagged/javafx" class="post-tag" title="show questions tagged &#39;javafx&#39;" rel="tag">javafx</a>.</p>
  
  <pre><code>SerialPort comPorts[] = SerialPort.getCommPorts();
  MenuItem[] portsItems = new MenuItem[10];
  int q=0;
     for (SerialPort port : comPorts) {
         portsItems[q] = new MenuItem(port.getSystemPortName());
         portsItems[q].setOnAction(actionEvent -&gt; {
                 portNum = q;
                 connect.setDisable(false);
         });
         comPortsMenu.getItems().add(portsItems[q]);
     }
  </code></pre>
  
  <p>The problem is I need to increment q in each loop, but I can't do that because <code>q</code> must be <strong><em>final</em></strong> or <strong><em>effectively final</em></strong> to be used inside a lambda.</p>