title: Referencing a java class in its own constructor
body: <p>I'm building a Java Swing class called ListView that attempts to be a general purpose list. </p>
  
  <pre><code>public class ListView&lt;T&gt; extends JPanel {
      private IListViewDataSource&lt;T&gt; dataSource;
      private JPanel list;
  
      public ListView(IListViewDataSource&lt;T&gt; dataSource, Dimension dimension) {
          this.dataSource = dataSource;
  
          list = new JPanel(new GridLayout(0, 1));
          this.add(new JScrollPane(list));
  
  
          this.setPreferredSize(dimension);
      }
  
      public void loadRows() {
          for (int i = 0; i &lt; dataSource.getNumberOfElements(); i++) {
              JLabel label = new JLabel(dataSource.getTitleOfElement(dataSource.getElementAtPosition(i)));
              list.add(label);
          }
      }
  }
  </code></pre>
  
  <p>In order to do this, I declared an interface called IListViewDataSource that defines the methods required for the list view to obtain its data.</p>
  
  <pre><code>public interface IListViewDataSource&lt;T&gt; {
      T getElementAtPosition(int position);
      int getNumberOfElements();
      String getTitleOfElement(T element);
  }
  </code></pre>
  
  <p>I wanted it to be possible to instantiate a new ListView with whichever DataSource you declare, in order to introduce whichever data in the list. So far so good.</p>
  
  <p>Now, I'm building another class called OfferListView that extends ListView, and in order not to have an inneccessary extra file I wanted it to implement its own ListViewDataSource. The problem is that I can't call <code>super(this, dimension)</code> inside the constructor for this new class, as I'm then told that <code>this</code> can't be used before the superclass constructor has been called.</p>
  
  <p>This "pattern" is what is used when programming with UIKit for iOS, and I think it's quite nice, but I can't get it to work in Java. How could I approach this?</p>