2

I understand, thanks to this question, that the value of a static field declared in an abstract class will be the same among all subclasses.

The solution in the aforementioned question is to declare a static field in each subclass, and an abstract "getter" instance method in the abstract class that must be implemented by each subclass.

But I have a static method in my abstract class, and I need to refer to the static field of the subclass. I can't do this because the getter is an instance method.

What's the best solution here? I'd rather not put nearly identical instances of getAll in every subclass.

public abstract class AbstractModel {

  public abstract String getTableName();

  public static ResultSet getAll() {

    Statement stmt = Database.get().conn.createStatement();

    // Error below: Cannot use "this" in static context.
    String query = "SELECT * FROM `" + this.getTableName() + "`";

    return stmt.executeQuery(query);
  }

}

public class Api extends AbstractModel {

  protected static final String TABLE_NAME = "apis";

  @Override
  public String getTableName() {
    return TABLE_NAME;
  }

}
Cameron Hudson
  • 3,190
  • 1
  • 26
  • 38
  • If you want a different value for each subclass, why make it `static` in the first place? – Tiberiu Mar 08 '19 at 22:07
  • Given that `getAll()` is static, it doesn't have any context. If you write `Api.getAll()`, that resolves to the same bytecode as `AbstractModel.getAll()` - there's no concept of calling the static method "on" the subclass. Perhaps you need to have a second class hierarchy, of `AbstractTable`, `ApiTable` etc... – Jon Skeet Mar 08 '19 at 22:08

1 Answers1

1

I was able to write the code in this way, to minimize repitition. It also eliminates the need for a getter.

public abstract class AbstractModel {

  public static ResultSet getAllFromTable(String tableName) {

    Statement stmt = Database.get().conn.createStatement();

    String query = "SELECT * FROM `" + tableName + "`";

    return stmt.executeQuery(query);
  }

}

public class Api extends AbstractModel {

  protected static final String TABLE_NAME = "apis";

  public static ResultSet getAll() {
    return getAllFromTable(TABLE_NAME);
  }

}
Cameron Hudson
  • 3,190
  • 1
  • 26
  • 38