title: Instantiate a generic class T object and return it
body: <p>I'm working on java selenium tests and I am trying to setup a fluent/method-chaining design code:</p>
  <p>I have a generic button class that allows navigating from a page class to the other. Buttons are attributes of my pages classes. Method in the button class should be able to return generic page classes that were defined on button instantiation (the page you are on, the page the button leads to etc.).
  But I can't figure how to do it without breaking the fluent design in the main class:</p>
  <p><code>Button.java</code></p>
  <pre><code>public class Button&lt;T, U&gt; {
      public T observe() {
          return new T(); // Does not work
      }
  
      public U click() {
          return new U(); // Does not work
      }
  }
  </code></pre>
  <p><code>FirstPage.java</code></p>
  <pre><code>public class FirstPage {
      public Button&lt;FirstPage, SecondPage&gt; buttonOnFirstPage = new Button&lt;&gt;();
  }
  </code></pre>
  <p><code>SecondPage.java</code></p>
  <pre><code>public class SecondPage {
      public Button&lt;SecondPage, AnotherPage&gt; buttonOnSecondPage = new Button&lt;&gt;();
  }
  </code></pre>
  <p>And finally what I want to be able to do without casting or mentioning types, in order to benefit from autocompletion and other fancy IDE stuff:
  <code>BrowsingTest.java</code></p>
  <pre><code>public class BrowsingTest {
      public static void main(String[] args) {
          new FirstPage()
              .buttonOnFirstPage.observe()
              .buttonOnFirstPage.click()
              .buttonOnSecondPage.observe()
              .buttonOnSecondPage.click(); //etc.
      }
  }
  </code></pre>
  <p>Any idea on how I can build that button class? Thanks a lot!</p>