title: Parameters passing in constructors - Java
body: <p>I have a <code>Person</code> class to describe some features of a person:</p>
  
  <pre><code>public class Person {
  
      private String name;
      private String surname;
      private String birthPlace;
      private int age;
  
      public Person(String name, String surname, String birthPlace, int age) {
          super();
          this.name = name;
          this.surname = surname;
          this.birthPlace = birthPlace;
          this.age = age;
      }
      public String getName() {
          return name;
      }
      public String getSurname() {
          return surname;
      }
      public String getBirthPlace() {
          return birthPlace;
      }
      public int getAge() {
          return age;
      }
  }
  </code></pre>
  
  <p>In <code>PersonDemo</code> class I create 4 people and I add them to an <code>ArrayList&lt;Person&gt;</code>. Then I pass the <code>ArrayList&lt;Person&gt;</code> to a <code>Frame</code> class (that extends <code>JFrame</code>):</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class PersonDemo {
  
      public static void main(String[] args) {
  
          Person p1 = new Person("Cristiano","Ronaldo","Santo Antonio", 31);
          Person p2 = new Person("Zlatan","Ibrahimovic","Malmo", 34);
          Person p3 = new Person("Alessandro","Del Piero", "Conegliano", 41);
          Person p4 = new Person("Zinedine","Zidane","Marseille",43);
  
          //ArrayList&lt;Person&gt;
          ArrayList&lt;Person&gt; people = new ArrayList&lt;&gt;();
          people.add(p1);
          people.add(p2);
          people.add(p3);
          people.add(p4);
  
          //Create and make visible a new Frame() f passing 
          //as parameter the previous ArrayList&lt;Person&gt;
          Frame f = new Frame(people);
          f.setVisible(true);
      }
  }
  </code></pre>
  
  <p>I split my frame with a <code>JSplitPane</code>:</p>
  
  <pre><code>import java.util.ArrayList;
  
  import javax.swing.JFrame;
  
  public class Frame extends JFrame{
  
      private static final long serialVersionUID = 1L;
      private static final int X_AXIS = 175;
      private static final int Y_AXIS = 25;
      private static final int WIDTH= 930;
      private static final int HEIGHT = 600;
  
      private SplitPane splitPane;
  
      public Frame(ArrayList&lt;Person&gt; p) {
  
          this.setTitle("My Frame");
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setBounds(X_AXIS,Y_AXIS,WIDTH,HEIGHT);
  
          //pass to the splitPane the ArrayList&lt;Person&gt; p
          splitPane = new SplitPane(p);
          this.getContentPane().add(splitPane);   
  
      }
  }
  </code></pre>
  
  <p>In the left side of <code>JSplitPane</code> I create a new <code>JPanel</code>; in this <code>JPanel</code> I have to work on the <code>ArrayList&lt;Person&gt;</code>:</p>
  
  <pre><code>import java.util.ArrayList;
  import javax.swing.JPanel;
  import javax.swing.JSplitPane;
  
  public class SplitPane extends JSplitPane{
  
      private static final long serialVersionUID = 1L;
      private static final int SPLIT_WEIGHT  = 250;
      private JPanel leftPanel;
  
      public SplitPane(ArrayList&lt;Person&gt; p) {
  
          //pass to the JPanel leftPanel the ArrayList&lt;Person&gt; p
          leftPanel = new LeftPanel(p);
          this.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
          this.setDividerLocation(SPLIT_WEIGHT);
          this.setLeftComponent(leftPanel);
      }
  }
  </code></pre>
  
  <p>This is my <code>LeftPanel</code> class that extends <code>JPanel</code>:</p>
  
  <pre><code>import java.util.ArrayList;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  public class LeftPanel extends JPanel{
  
      private static final long serialVersionUID = 1L;
  
      public LeftPanel(ArrayList&lt;Person&gt; p) { 
  
          JLabel label = new JLabel("This is the left side of \"My Frame\"");
          this.add(label);
  
          //some code to work with ArrayList&lt;Person&gt; p in this JPanel 
      }
  }
  </code></pre>
  
  <p>I wonder whether to pass the <code>ArrayList&lt;Person&gt;</code> in constructors (<code>JFrame -&gt; JSplitPane -&gt; JPanel</code>) is a good approach or there are better solutions (maybe inheritance).</p>