title: Variable cannot be called outside of MouseListener constructor
body: <p>I'm trying to create a Java program utilizing Swing. One of the things I'm trying to accomplish is using a MouseListener to get the index of a clicked item in a JList and retrieve a variable associated with index of the array. My problem is that when I try to call the variable outside of the MouseListener, it does not get recognized. My code being: </p>
  
  <pre><code>public class UserListPanel extends JPanel {
  
  LibraryController ctrl = new LibraryController();
  JScrollPane scrollpane;
  public int userid;
  public String userName;
  
  public UserListPanel(final Borrower[] borrowersArray) {
  
      String userArray [] = new String [borrowersArray.length];
      for (int i = 0; i &lt; userArray.length; i++) {
          userArray[i] = borrowersArray[i].getName();
      }
  
      JList userList = new JList(userArray);
      scrollpane = new JScrollPane(userList);
      this.add(scrollpane);
  
      // Adds a mouse click listener to assign values from the JList to a variable on click
      userList.addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent evt) {
              JList userList = (JList)evt.getSource();
              if (evt.getClickCount() &gt;= 0) {
                  int index = userList.locationToIndex(evt.getPoint());
                  ListModel dlm = userList.getModel();
                  Object item = dlm.getElementAt(index);
                  userList.ensureIndexIsVisible(index);
                  userid = borrowersArray[index].getbID();
                  userName = borrowersArray[index].getName();
                  JOptionPane.showMessageDialog(null, userName);
              }
          }
      });
  }
  
  userid = borrowersArray[index].getbID();
  
  }
  </code></pre>
  
  <p>Within the MouseListener constructor I am able to correctly get the variable and store it within the userid variable, for example, and my JOptionPane confirms this by returning a number. Hoewever, outside of the constructor, the integer "index" is not recognized and thus if I were to call userid, it would return null. How would I go about having a copy of index outside of MouseListener?</p>