title: java - How to create an instance of generic type T
body: <p>I am writing a server like below</p>
  
  <pre><code>public class Server&lt;T extends RequestHandler&gt; {
  
      public void start() {
  
          try{
              this.serverSocket = new ServerSocket(this.port, this.backLog);
          } catch (IOException e) {
              LOGGER.error("Could not listen on port " + this.port, e);
              System.exit(-1);
          }
  
          while (!stopTheServer) {
              socket = null;
              try {
                  socket = serverSocket.accept();
                  handleNewConnectionRequest(socket);
              } catch (IOException e) {
                  LOGGER.warn("Accept failed at: " + this.port, e);
                  e.printStackTrace();
              }
          }
  
      }
  
      protected void handleNewConnectionRequest(Socket socket) {
          try {
              executorService.submit(new T(socket));
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>But in the <code>handleNewConnectionRequest(...)</code> method, I can't create an instance of T as it is actually not a class. Also I can't use the method mentioned <a href="https://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java">here</a> as I want to pass the <code>socket</code> instance so that the request handler can get <code>OutputStream</code> and <code>InputStream</code> on the <code>socket</code>.</p>
  
  <hr>
  
  <p>Can't I make a generic server like above and have different protocol handlers e.g</p>
  
  <pre><code>public class HttpRequestHandler extends RequestHandler {
      ...
  }
  
  public class FtpRequestHandler extends RequestHandler {
      ...
  }
  
  public class SmtpRequestHandler extends RequestHandler {
      ...
  }
  </code></pre>
  
  <p>and then use them like below</p>
  
  <pre><code>Server&lt;HttpRequestHandler&gt; httpServer = new Server&lt;HttpRequestHandler&gt;();
  Server&lt;FtpRequestHandler&gt; ftpServer = new Server&lt;FtpRequestHandler &gt;();
  Server&lt;SmtpRequestHandler&gt; smtpServer = new Server&lt;SmtpRequestHandler &gt;();
  </code></pre>