title: How can I pass a non final variable to an anonymous inner class?
body: <p>I have these lines of code. I know you can not pass a non final variable to an inner class but I need to pass the variable <code>i</code> to the anonymous inner class to be used as a seatingID. Can you suggest ways of doing that ?</p>
  
  <pre><code>JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
  for (int i = 0; i &lt; 40; i++)
  {
      seats[i] = new JButton();//creating the buttons
      seats[i].setPreferredSize(new Dimension(50,25));//button width
      panel4seating.add(seats[i]);//adding the buttons to the panels
  
      seats[i].addActionListener(new ActionListener()
      {  //anonymous inner class
          public void actionPerformed(ActionEvent evt)
          {  
              String firstName = (String)JOptionPane.showInputDialog("Enter First Name");
              String lastName = (String)JOptionPane.showInputDialog("Enter Last Name");
  
              sw101.AddPassenger(firstName, lastName, seatingID);
          }
      });
  }
  </code></pre>