senzacionale
senzacionale

Reputation: 20906

java generic how to use generic in constructor

public class DbManager<T extends Dao> {

    public DbManager(Context c) {
        setCreateDatabaseScript(T);
    }

    public void setCreateDatabaseScript(T dao) {
        this.createDatabaseScript = ((Dao)dao).createTable();
    }

    //..
}

how can I inside constructor call setCreateDatabaseScript. I try like in example but not working. What is correct syntax?

Upvotes: 0

Views: 73

Answers (3)

Warren Green
Warren Green

Reputation: 58

The object, T, has never been declared or instantiated when it is used in your constructor.

Upvotes: 0

Pshemo
Pshemo

Reputation: 124215

setCreateDatabaseScript(T dao) needs instance of T so you probably should pass it in constructor

public DbManager(Context c, T dao) {
    setCreateDatabaseScript(dao);
}

Also since T extends Dao you don't need to cast it to Dao, just use dao.createTable();

Upvotes: 1

Cyrille Ka
Cyrille Ka

Reputation: 15523

public DbManager(Context c) {
    setCreateDatabaseScript(T);
}

You have to call setCreateDatabaseScript with an object of type T as parameter, not just with T.

Upvotes: 0

Related Questions