title: Create independent objects of a class
body: <p>I want to create more than one object of a class that are independent one from the other.</p>
  
  <p>I call class constructor multiple time in a for loop, but how can I identify this objects?</p>
  
  <p>I tried using a static variable that takes the value of the for loop index, but this variable assume the value of last index of the for loop.</p>
  
  <p>Here is the code:</p>
  
  <p>Maingui.java</p>
  
  <pre><code>public class Maingui extends JFrame {
  
      public static JFrame frame;
      public static JButton runButton;
  
      public Maingui() throws IOException {
  
          frame = new JFrame("maingui");
          setSize(1024,700);
          setTitle("maingui");
          setLocationRelativeTo(null);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setVisible(true);
  
          setLayout(new BorderLayout());
  
          runButton = new JButton("Run");
          runButton.addActionListener(new RunBtnListener());
          add(runButton);
  
      }
  }
  </code></pre>
  
  <p>RunBtnListener.java</p>
  
  <pre><code>public class RunBtnListener implements ActionListener {
  
      ArrayList&lt;SecondGui&gt; menus = new ArrayList&lt;SecondGui&gt;();
  
      public void actionPerformed(ActionEvent e) {
  
          for(int i = 0; i &lt; 2; i++) {
  
              menus.add(new SecondGui(i));
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>SecondGui.java</p>
  
  <pre><code>public class SecondGui extends JFrame {
  
      public static int c;
      JFrame frame;
      JButton button;
  
      public SecondGui(int i) {
  
          this.c = i;
  
          frame = new JFrame("Test");
          setSize(1024,700);
          setTitle("Menu");
          setLocationRelativeTo(null);
          setVisible(true);
          this.setLocation(50*i, 50*i);
  
          // Set layout manager
          setLayout(new BorderLayout());
  
          button = new JButton("B");
          button.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  System.out.println("C: " + c);
  
              }
  
          });
  
          add(button);
  
      }
  
  }
  </code></pre>
  
  <p>When I click on the button <code>button</code> in <code>SecondGui</code> GUI, the output is always <code>1</code>.
  But I want that the output will be <code>0</code> or <code>1</code> based on in which GUI I select the button.</p>