title: calling a method thats located in main class from another class
body: <p>I have a method in the class that holds main called <code>addNewProduct(String name)</code> In one of my JPanels i have a JButton that i have an <code>actionPerformed()</code> this is in another class called <code>DesktopGUI</code>. main creates a copy of <code>DesktopGUI</code> and that object makes the frame and all the components. I would like to have a way to call methods in the main class from the <code>DesktopGUI</code>. </p>
  
  <p>main class (this is the lines of code that are in question. This is not a full representation of my code. Lines of code may also not be in the order they would be in my real application.)</p>
  
  <pre><code>   public List&lt;Product&gt; productList;
      public void addNewProduct(String name){
        Product product = new Product();
        product.setName(name);
        productList.add(product);
      }
  
      DesktopGUI gui = new DesktopGUI();
      frame = gui.getFrame();
  </code></pre>
  
  <p>In the constructor of <code>DesktopGUI</code> it creates and displays the frame. </p>
  
  <p>in a section of code i have</p>
  
  <pre><code>SaveButton.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent event) {
        System.out.println("Save Button Pressed");
        System.out.println("Name: "+(String)fileNameField.getText());
        /*the addNewProduct() is located in the class for main*/
        addNewProduct((String)fileNameField.getText());
     }
  });
  </code></pre>
  
  <p>I was able to call the class if it was static but then the class productList will always have a size of zero. </p>
  
  <p>given the simple example</p>
  
  <pre><code>public class Foo {
     public List&lt;Product&gt; productList;
     public static void main(String[] args) {
        A a = new A();
  
     }
     public void addNewProduct(String name){
        Product product = new Product();
        product.setName(name);
        productList.add(product);
     }
  }
  
  class A {
     SaveButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent event) {
            foo.addNewProduct("Hello World");
         }
      });
  }
  </code></pre>