title: Access variables in the actionPerformed
body: <p>I have to define a variable called 'path' as can be seen in the code inside the ActionListener...but I can not access 'path' variable outside the actionPerformed method! It becomes null outside the method...How can i access this variable?!</p>
  
  <pre><code>button.addActionListener(new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent e) {
  
              System.out.println("Search " + textField.getText()
                      + " in Project");
  
              try {
                  System.out.println(project.members().length);
                  IResource[] ires = project.members();
                  String path = "";
                  String findResult = "notFound";
                  for (int len = 0; len &lt; ires.length; len++) {
                      if (!(path = loopInFolders(project, ires[len],
                              textField.getText())).equals("")) {
                          System.out.println("found at :" + path);
                          findResult = "found";
  
                          showResultBox(findResult, path);
  
                          break;
                      }
                  }
  
                  if (path.equals("")) {
                      showResultBox(findResult, path);
                  }
              } catch (CoreException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
  
  
          }
      });
  </code></pre>
  
  <p>Even I defined the variable as global variable in the class with getter and setter..but the variable does not keep its value which it has inside actionPerformed! Only inside the Listener has the correct value which i want...I want to use this value in other methods, but it becomes null there! </p>
  
  <p>This is my class! I want to access the path variable in execute method, this variable will be filled in actionListener inside createOutput method, but it is always null outside the actionListener!</p>
  
  <pre><code>public class FindHandler extends AbstractHandler {
  
  private String path;
  
  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
  
      Shell shell = HandlerUtil.getActiveShell(event);
      ISelection sel = HandlerUtil.getActiveMenuSelection(event);
  
      IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
      IProject project = root.getProject(sel.toString()
              .substring(1, sel.toString().indexOf(" ")).trim());
      System.out.println("selected folder "
              + sel.toString().substring(1, sel.toString().indexOf(" "))
                      .trim());
  
  
  
      createOutput(shell, project);
  
      if (path != null) {
          System.out.println("Pathhhh***444"+ path);
          IPath iPath = new Path(path);
          IFile file = project.getFile(iPath);
          System.out.println("test file*****" + file.getName());
  
          file = ResourcesPlugin.getWorkspace().getRoot()
                  .getFileForLocation(iPath);
  
          ISelection selection = new StructuredSelection(file);
  
          IViewReference[] views = PlatformUI.getWorkbench()
                  .getActiveWorkbenchWindow().getActivePage()
                  .getViewReferences();
          PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                  .getActivePage().resetPerspective();
  
          for (IViewReference view : views) {
              if ("org.eclipse.jdt.ui.PackageExplorer".equals(view.getId())) {
                  IViewPart pExplorer = view.getView(true);
                  pExplorer.getViewSite().getSelectionProvider()
                          .setSelection(selection);
                  break;
              }
          }
      }
  
      return null;
  }
  
  private void createOutput(Shell shell, final IProject project) {
  
      // Creating the window with a textBox and two buttons of 'Search' and
      // 'Cancel'
      System.out.println(project.getLocation());
      final JTextField textField = new JTextField();
      final JFrame frame = new JFrame("Search File");
      frame.setLayout(null);
      frame.setSize(350, 250);
      frame.setLocation(350, 250);
      JLabel label = new JLabel("Enter File Name:");
      label.setSize(120, 20);
      label.setLocation(20, 25);
      textField.setSize(150, 20);
      textField.setLocation(150, 25);
      frame.getContentPane().add(textField);
      frame.getContentPane().add(label);
      JButton button = new JButton("Search");
      button.setSize(100, 30);
      button.setLocation(70, 100);
      button.addActionListener(new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent e) {
  
              System.out.println("Search " + textField.getText()
                      + " in Project");
  
              try {
                  System.out.println(project.members().length);
                  IResource[] ires = project.members();
                  path = "";
                  String findResult = "notFound";
                  for (int len = 0; len &lt; ires.length; len++) {
                      if (!(path = loopInFolders(project, ires[len],
                              textField.getText())).equals("")) {
                          System.out.println("found at :" + path);
                          findResult = "found";
  
                          showResultBox(findResult, path);
  
                          break;
                      }
                  }
  
                  if (path.equals("")) {
                      showResultBox(findResult, path);
                  }
              } catch (CoreException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
  
  
          }
      });
      frame.getContentPane().add(button); // Adds Button to content pane of
                                          // frame
      JButton button_2 = new JButton("Cancel");
      button_2.setSize(100, 30);
      button_2.setLocation(180, 100);
      button_2.addActionListener(new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent e) {
              // TODO Auto-generated method stub
              frame.dispose();
          }
      });
      frame.getContentPane().add(button_2);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  
      System.out.println("Pathhhh***333"+ path);
  
  }
  
  
  public String loopInFolders(IProject project, IResource ires,
          String fileName) {
  
      if (ires.getName().equals(fileName)) {
          return ires.getLocation().toString();
      } else {
          System.out.println(ires.getName());
          IFolder secondFolder = null;
          try {
              secondFolder = project.getFolder(ires.getName());
              System.out.println(secondFolder.members().length);
              if (secondFolder.members().length &gt; 0) {
                  IResource[] ires1 = secondFolder.members();
                  for (int i = 0; i &lt; ires1.length; i++) {
                      if (!loopInFolders(project, ires1[i], fileName).equals(
                              ""))
                          return ires1[i].getLocation().toString();
                  }
              }
          } catch (Exception ex) {
          }
      }
      return "";
  
  }
  
  public void showResultBox(String findResult, String path) {
  
      if (findResult.equals("notFound")) {
  
          final JFrame frame = new JFrame("File Not Found");
          frame.setLayout(null);
          frame.setSize(350, 250);
          frame.setLocation(350, 250);
          JLabel label = new JLabel("There is no such file in the project!");
          label.setSize(600, 20);
          label.setLocation(20, 25);
  
          frame.getContentPane().add(label);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  
      } else {
  
          System.out.println("Pathhhh***111"+ path);
  
          final JFrame frame = new JFrame("File Found");
          frame.setLayout(null);
          frame.setSize(350, 250);
          frame.setLocation(350, 250);
          JLabel label = new JLabel("File is found at: " + path);
          label.setSize(600, 20);
          label.setLocation(20, 25);
  
          frame.getContentPane().add(label);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  
      }
  
  }
  </code></pre>
  
  <p>}</p>