title: How to make a listenener relation not recursiv
body: <p>I broke down my code to the base of my theoretical problem</p>
  
  <pre><code>public class main {
  
      public static void main(String[] args) {    
          new Object1().a();
      }
  }
  
  public class Object1 {
  
      private static Object2 object2;
  
      public Object1() {
  
      }
  
      public void a(){
  
          object2 = new Object2(this);
          object2.setVisible(true);
          System.out.println("new object2 created");
  
      }
  
      public void b(){
  
          object2 = null;
          System.out.println("set object2 to null");
          a();
  
      }
  }
  
  /**
   *
   *Class creates JFrame with a button.
   *the button has an event listener which calls method b form object1
   *
   */
  
  public class Object2 extends javax.swing.JFrame {
  
      private JButton jButton1;
      private Object1 object1;
  
      public Object2(Object1 ob1) {
  
          super();
          this.object1 = ob1;
          initGUI();
  
      }
  
      private void initGUI() {
          try {
              setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
              {
                  jButton1 = new JButton();
                  getContentPane().add(jButton1, BorderLayout.CENTER);
                  jButton1.setText("click me");
                  jButton1.setPreferredSize(new java.awt.Dimension(311, 143));
                  jButton1.addActionListener(new ActionListener() {
                      public void actionPerformed(ActionEvent evt) {
  
                          jButton1ActionPerformed(evt);
                      }
                  });
              }
              pack();
              this.setSize(150, 125);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      private void jButton1ActionPerformed(ActionEvent evt) {
  
          System.out.println("button clicked");
  
          object1.b();
  
      }
  </code></pre>
  
  <p>the call of my jFrame is most likely a recursion, the consequence is that setting the Frame null won't do anything ...
  the Frame is still visible and works without throwing a NullPointerException.
  But thats not the center of the problem.</p>
  
  <p>The Question is :
  How can I write an equivalent code to this without a recursion?</p>