0

I have two classes:

public abstract class Entry {
    private static String LIST_QUERY     = "SELECT * FROM \"%s\"";
    .
    .
    .
    protected static <T extends Entity> List<T> all(Class<T> cls) {
/*Here I want to load whole database. For each row create own object. For example Article(class is below)*/
   }
}

class Article extends Entry {
   .
   .
   .
    public static List<Article> all() throws SQLException {
        return Entry.all(Article.class);
   }
}

Question is how inside Entry.all() method create Article objects or any object inherited from Entry? My variants are wrong:

cls<T> instance_of_entity = new cls<T>();//wrong
cls instance_of_entity = new cls();//wrong
ovod
  • 1,118
  • 4
  • 18
  • 33
  • See http://stackoverflow.com/questions/2434041/instantiating-generics-type-in-java – rodrigoap Mar 28 '15 at 19:25
  • You can use cls.newInstance(). More info here: https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html. But this requires passing in the runtime class (in the cls variable). Because of type erasure you cannot instantiate with T alone. – Mick Mnemonic Mar 28 '15 at 20:05
  • But how to create object? Object instance = cls.newInstance();? or Object instance = cls.newInstance(); or Entity instance = cls.newinstance();? – ovod Mar 28 '15 at 23:04

0 Answers0