title: Java Swing Calculator - Lack of main method
body: <p>Today I wrote a graphical calculator, which operates through the use of buttons only, and after finishing I realized I hadn't defined a main method throughout my code. I've been thinking about it for a while but can't, with my limited knowledge, come up with a way to work around this issue.</p>
  
  <p>I was thinking of replacing the method which I'm using for construction of the calculator with the main, but ran into an issue with accessing the non-static variables outside the main.</p>
  
  <p>My code for reference:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class GraphicalCalculator{
  
      // Hold first and second numbers, to be acted upon
      private double num1 = 0;
      private double num2 = 0;
      private char function = ' ';
  
      // User's 'screen'
      private JTextField Scr;
  
      public GraphicalCalculator(){
  
          // Numerical Buttons (0-9)
          JButton[] bNum = new JButton[10];
          for(int i = 9; i &gt;= 0; i--){
              bNum[i] = new JButton(Integer.toString(i));
          }
  
          // Function buttons
          JButton bClear = new JButton("Clear");
          JButton bCalc = new JButton("Calculate");
          JButton bAdd = new JButton("+");
          JButton bSub = new JButton("-");
          JButton bMul = new JButton("*");
          JButton bDiv = new JButton("/");
  
          // TextField which acts as user's screen
          Scr = new JTextField("");
          Scr.setEnabled(false);
  
          // Calculator frame, contains entire GUI
          JFrame frame = new JFrame("Calculator");
              frame.setSize(300, 400);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
  
          // Main pane, contains 3 sub-panes
          JPanel pane = (JPanel) frame.getContentPane();
              pane.setLayout(new GridLayout(3, 1));
  
          // Add numerical buttons to Panel
          JPanel numButtons = new JPanel();
              numButtons.setLayout(new GridLayout(4, 3));
              for(int i = 9; i &gt;= 0; i--){
                  numButtons.add(bNum[i]);
              }
  
          // Add function buttons to Panel
          JPanel fncButtons = new JPanel();
              fncButtons.setLayout(new GridLayout(3, 2));
              fncButtons.add(bAdd);
              fncButtons.add(bSub);
              fncButtons.add(bMul);
              fncButtons.add(bDiv);
              fncButtons.add(bClear);
              fncButtons.add(bCalc);
  
          // Implementing action listeners for each numerical button
          bNumAction[] bNumActions = new bNumAction[10];
          for(int i = 0; i &lt;= 9; i++){
              bNumActions[i] = new bNumAction(bNum[i]);
              bNum[i].addActionListener(bNumActions[i]);
          }
  
          // Implementing action listeners for each function button
          bAdd add = new bAdd();
              bAdd.addActionListener(add);    
          bSub sub = new bSub();
              bSub.addActionListener(sub);        
          bMul mul = new bMul();
              bMul.addActionListener(mul);        
          bDiv div = new bDiv();
              bDiv.addActionListener(div);        
          bClear clear = new bClear();
              bClear.addActionListener(clear);            
          bCalc calc = new bCalc();
              bCalc.addActionListener(calc);
  
          pane.add(Scr);
          pane.add(numButtons);
          pane.add(fncButtons);
          frame.add(pane);
      }
  
      private class bNumAction implements ActionListener{
  
          // Holds value of button pressed
          private String temp;
  
          // Retrieve value of button pressed
          public bNumAction(JButton pressed){
              this.temp = pressed.getText();
          }
  
          // If there is currently a value on the screen, add it it, otherwise set screen value to input
          public void actionPerformed(ActionEvent e){
              if(!Scr.getText().equals("")){
                  Scr.setText(Scr.getText() + temp);
              } else {
                  Scr.setText("");
                  actionPerformed(e);
              }
          }
      }
  
      // Following 4 classes transmit function key pressed to numIn
      private class bAdd implements ActionListener{
          public void actionPerformed(ActionEvent e){
              numIn('+');
          }
      }
  
      private class bSub implements ActionListener{
          public void actionPerformed(ActionEvent e){
              numIn('-');
          }
      }
  
      private class bMul implements ActionListener{
          public void actionPerformed(ActionEvent e){
              numIn('*');
          }
      }
  
      private class bDiv implements ActionListener{
          public void actionPerformed(ActionEvent e){
              numIn('/');
          }
      }
  
      // Stores value of input number and function used, then clears for next input
      public void numIn (char fnc){
          if(num1 == 0){
              num1 = Double.parseDouble(Scr.getText());
              Scr.setText("");
          } else {
              num2 = Double.parseDouble(Scr.getText());
              Scr.setText("");
          }
          function = fnc;
      }
  
      // Clears all values if Clear button is pressed
      private class bClear implements ActionListener{
          public void actionPerformed(ActionEvent e){
              Scr.setText("");
              num1 = 0;
              num2 = 0;
              function = ' ';
          }
      }
  
      // Calculates result when Calculate button is pressed, stores result as num1 for use in continued calculation
      private class bCalc implements ActionListener{
          public void actionPerformed(ActionEvent e){
              num2 = Double.parseDouble(Scr.getText());
              if(function == '+'){
                  Scr.setText(Double.toString((Math.round((num1 / num2) * 100)) / 100));
              } else if(function == '-'){
                  Scr.setText(Double.toString(num1 - num2));
              } else if(function == '*'){
                  Scr.setText(Double.toString(num1 * num2));
              } else if(function == '/'){
                  Scr.setText(Double.toString(num1 / num2));
              } else {
                      Scr.setText(String.valueOf(num1));
              }
              num1 = Double.parseDouble(Scr.getText());
          }
      }
  }
  </code></pre>
  
  <p>Any solutions or advice for workarounds would be greatly appreciated.</p>