I’ve built a class and would like to include a constructor which will construct the class from a cursor which will point to an SQLite DB.
I also have a database helper class which sits in the same package and has static variables which give labels to the columns in the cursor.
Should I use these references in the the constructor of the class or is that bad practice?
Thanks,
m
here is a simple generic example asked for in the comments…
public class carDbHelper extends SQLiteOpenHelper{
public static final int ROW_ID = 0;
public static final int ROW_TYPE = 1;
...// all db helper code omitted
}
public class Car{
private int id;
private String type;
public Car (Cursor c){
this.id = c.getInt(carDbHelper.ROW_ID);
this.id = c.getString(carDbHelper.TYPE_ID);
}
//Other code omitted
}
Either copying object or referencing to object is good, the problem is that you need to control the object life time well.
I personally prefer copying object to referencing it, since it’s really hard to know when object starts to exist or destroy if there’re many continuous references on the same object.