title: How do I access variables from the main class from another class (java)?
body: <p>I'm making a cookie clicker clone in java to practice my java skills and I have a small problem, I have variables that are declared in the main method that I want to access from an ActionListener class. Here is some sample code from the ActionListener class. the the int variables (ex. clicks, grandamaCost) and the JTextFields (ex. display, cpsDisplay) are all in the main method. I was wondering how I could have access to variables in the main method so that this code could work in the other class. Thanks!</p>
  
  <pre><code>@Override
  public void actionPerformed(ActionEvent e) {
      JButton b = (JButton) e.getSource();
      button(b.getText());
  }
  
  public void button(String input) {
      switch (input) {
          case "Cookie":
              clicks++;
              display.setText("Cookies: " + clicks + "");
              cpsDisplay.setText("CPS: " + cps);
              break;
          case "Buy grandma":
              if (clicks &gt;= grandmaCost) {
                  grandmas++;
                  clicks = clicks - grandmaCost;
                  grandmaCost = (int) ((.15 * grandmaCost) + grandmaCost);
                  cps++;
              }
              display.setText("Cookies: " + clicks + "");
              prices[0].setText("$" + grandmaCost);
              cpsDisplay.setText("CPS: " + cps);
              break;
          case "Buy monkey":
              if (clicks &gt;= monkeyCost) {
                  monkeys++;
                  clicks = clicks - monkeyCost;
                  monkeyCost = (int) ((.15 * monkeyCost) + monkeyCost);
                  cps = cps + 2;
              }
              display.setText("Cookies: " + clicks + "");
              prices[1].setText("$" + monkeyCost);
              cpsDisplay.setText("CPS: " + cps);
              break;
          case "Buy Teemo":
              if (clicks &gt;= teemoCost) {
                  teemos++;
                  clicks = clicks - teemoCost;
                  teemoCost = (int) ((.15 * teemoCost) + teemoCost);
                  cps = cps + 3;
              }
              display.setText("Cookies: " + clicks + "");
              prices[2].setText("$" + teemoCost);
              cpsDisplay.setText("CPS: " + cps);
              break;
      }
  }
  </code></pre>
  
  <p>}</p>