title: java generic constructors
body: <p>I currently have the following code that retrieves data from the database and then create a <code>User</code>. This code is used in many of my classe to create other objects such as <code>News</code>, <code>Comments</code>etc...</p>
  
  <p>It uses apache commons dbutils.</p>
  
  <pre><code>final ResultSetHandler&lt;User&gt; handler = new ResultSetHandler&lt;User&gt;() {
  
              @Override
              public User handle(ResultSet rs) throws SQLException {
  
                  User user = null;
                  if (rs.next()) {
                      user = new User();
                      user.setId(rs.getInt("id"));
                      user.setUsername(rs.getString("username"));
                      user.setPassword(rs.getString("password"));
                  }
                  return user;
              }
          };
  
          final User user = run.query(
                  "SELECT id, username, password FROM users WHERE username = ? AND active = 2 LIMIT 1;", handler,
                  username);
  </code></pre>
  
  <p>Would it be possible to wrap the <code>QueryRunner</code> in a generic class and override the query method so the handler instanciate the generic <code>T</code> with the ResultSet. I would make sure any <code>T</code>type would ahve a constructor accepting a <code>ResultSet</code>.</p>
  
  <p>Like so :</p>
  
  <pre><code>        public class QueryExecuter&lt;T&gt; extends QueryRunner {
      private ResultSetHandler&lt;T&gt; _handler;
  
      public QueryExecuter(){//The T type was for testing haha
          super();
          handler = new ResultSetHandler&lt;T&gt;() {
  
              @Override
              public T handle(ResultSet rs) throws SQLException {
  
                  T object = null;
                  if (rs.next()) {
                      object = new T(rs);
                  }
                  return object;
              }
          };
      }
  }
  </code></pre>
  
  <p>I don't know if you'll understand, but I hope so, ask me if you want more details or a better explanation.</p>
  
  <p>EDIT</p>
  
  <p>I thought I could use a AbstractClass instead of the generic type that all of the differents objects would extends but it seems like I can't write an abstract constructor. Will I have to make a static method that return an instance of the object like:</p>
  
  <pre><code>public abstract class DatabaseEntity {
      public static abstract DatabaseEntity create(ResultSet rs);//even this doesn't work...
  }
  </code></pre>