title: How can I refer to the class type a interface is implementing in Java?
body: <p>I came to a problem with interfaces in a program I'm making. I want to create a interface which have one of its methods receiving/returning a reference to the type of the own object. It was something like:</p>
  
  <pre><code>public interface I {
      ? getSelf();
  }
  
  public class A implements I {
      A getSelf() {
          return this;
      }
  }
  
  public class B implements I {
      B getSelf() {
          return this;
      }
  }
  </code></pre>
  
  <p>I can't use an "I" where it's a "?", because I don't want to return a reference to the interface, but the class. I searched and found that there are no way to "self-refer" in Java, so I can't just substitute that "?" in the example for a "self" keyword or something like this. Actually, I came up to a solution that goes like</p>
  
  <pre><code>public interface I&lt;SELF&gt; {
      SELF getSelf();
  }
  
  public class A implements I&lt;A&gt; {
      A getSelf() {
          return this;
      }
  }
  
  public class B implements I&lt;B&gt; {
      B getSelf() {
          return this;
      }
  }
  </code></pre>
  
  <p>But it really seems like a workaround or something alike. Is there another way to do so?</p>