title: Generifying with static factory methods and constants in Java
body: <p>In my Android app, I have some very similar classes, let's call them <code>FooA</code> and <code>FooB</code>.</p>
  
  <p>For each of these classes, I have a schema class that contains constants for the columns of the table - <code>FooASchema</code> and <code>FooBSchema</code>:</p>
  
  <pre><code>public final class FooASchema {
  
      public static final String TABLE_NAME = "foo_a_table";
      public static final String COL_CATEGORY_ID = "category_id";
      public static final String COL_PROPERTY_A = "property_a";
      public static final String COL_PROPERTY_B = "property_b";
      // COL_PROPERTY_C = ...
  
  }
  
  
  public final class FooBSchema {
  
      public static final String TABLE_NAME = "foo_b_table";
      public static final String COL_CATEGORY_ID = "category_id";
      public static final String COL_OTHER_PROPERTY_A = "other_property_a";
      // COL_OTHER_PROPERTY_B = ...
  
  }
  </code></pre>
  
  <p>Both <code>FooA</code> and <code>FooB</code> have a static factory method that enables me to create them using a <code>Cursor</code>:</p>
  
  <pre><code>public static FooA from(Cursor cursor) {
      int categoryId = cursor.getInt(cursor.getColumnIndex(FooASchema.COL_CATEGORY_ID));
      String propertyA = cursor.getString(cursor.getColumnIndex(FooASchema.COL_PROPERTY_A));
      String propertyB = cursor.getString(cursor.getColumnIndex(FooASchema.COL_PROPERTY_B));
      // int propertyC = ...
  
      return FooA(id, propertyA, propertyB, ...);
  }
  
  
  public static FooB from(Cursor cursor) {
      int categoryId = cursor.getInt(cursor.getColumnIndex(FooBSchema.COL_CATEGORY_ID));
      int otherA = cursor.getInt(cursor.getColumnIndex(FooASchema.COL_OTHER_PROPERTY_A));
      // String otherB = ...
  
      return FooB(id, otherA, otherB, ...);
  }
  </code></pre>
  
  <p>Finally, I have two util classes that I use to retrieve data from the tables:</p>
  
  <pre><code>public final class FooAUtils {
  
      public static ArrayList&lt;FooA&gt; getFooAs(Context context, int categoryId) {
          ArrayList&lt;FooA&gt; fooAs = new ArrayList&lt;&gt;();
  
          Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
                  FooASchema.TABLE_NAME,
                  null,
                  FooASchema.COL_CATEGORY_ID + "=?",
                  new String[] {String.valueOf(categoryId)},
                  null,
                  null,
                  null);
          cursor.moveToFirst();
          while (!cursor.isAfterLast()) {
              fooAs.add(FooA.from(cursor));
              cursor.moveToNext();
          }
          cursor.close();
          return fooAs;
      }
  
      // ...
  }
  
  
  public final class FooBUtils {
  
      public static ArrayList&lt;FooA&gt; getFooBs(Context context, int categoryId) {
          ArrayList&lt;FooB&gt; fooBs = new ArrayList&lt;&gt;();
  
          Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
                  FooBSchema.TABLE_NAME,
                  null,
                  FooBSchema.COL_CATEGORY_ID + "=?",
                  new String[] {String.valueOf(categoryId)},
                  null,
                  null,
                  null);
          cursor.moveToFirst();
          while (!cursor.isAfterLast()) {
              fooBs.add(FooB.from(cursor));
              cursor.moveToNext();
          }
          cursor.close();
          return fooBs;
      }
  
      // ...
  }
  </code></pre>
  
  <p>You can see that most of the code between <code>FooA</code>-related classes and <code>FooB</code>-related classes are very similar, and especially in the util classes - where the code is almost identical.</p>
  
  <p>I want to try to reduce this duplication, and I have been trying to do so using generics (I've read about them, but I haven't yet used them in a project).</p>
  
  <p>For example, I want to be able to have a generic util class. Here's how I thought I could implement it:</p>
  
  <pre><code>public final class FooUtils {
  
      public static &lt;T&gt; get(Context context, int categoryId) {
          ArrayList&lt;T&gt; items = new ArrayList&lt;&gt;();
  
          Cursor cursor = MyDbHelper.getInstance(context).getReadableDatabase.query(
                  BaseSchema.TABLE_NAME,
                  null,
                  BaseSchema.COL_CATEGORY_ID + "=?",
                  new String[] {String.valueOf(categoryId)},
                  null,
                  null,
                  null);
          cursor.moveToFirst();
          while (!cursor.isAfterLast()) {
              items.add(T.from(cursor)); // ??
              cursor.moveToNext();
          }
          cursor.close();
      }
  
      // ...
  
  }
  </code></pre>
  
  <p>Where:</p>
  
  <pre><code>public interface BaseSchema {
  
      public static final String TABLE_NAME; // can't make this abstract?
  
      public static final String COL_CATEGORY_ID = "category_id";
  
  }
  
  public final class FooASchema implements BaseSchema { ... }
  
  
  public final class FooBSchema implements BaseSchema { ... }
  </code></pre>
  
  <p>But as you can see, I can't do <code>T.from(cursor)</code>, and I can't have an abstract constant <code>TABLE_NAME</code> that the subclasses can implement.</p>
  
  <p>How can I call my static factory method in this way?</p>
  
  <p>Is there a better way of approaching this and reducing code duplication?</p>