Reputation: 169
I don't understand the following code snippet of how it works:
public class GroceryDBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "grocerylist.db";
public static final int DATABASE_VERSION = 1;
public GroceryDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
Namely: First, inheriting from the SQLiteOpenHelper class should call the constructor, identical to the constructor from this class. In this case, the constructor only accepts the context variable. But still works fine.
Secondly: my class inherits from the SQLiteOpenHelper class. In the SQLiteOpenHelper class, member variables are private, so they are not inherited by my class. So I don't have a member variable responsible for the database name.
Additionaly my constructor, which I gave earlier, does not initialize the database name, but when i use method dbHelper.getDatabaseName();
I am actually getting the correct database name. How is this happening?
Upvotes: 1
Views: 134
Reputation: 2220
This line: super(context, DATABASE_NAME, null, DATABASE_VERSION);
is calling the constructor for the parent class SQLiteOpenHelper
.
You have two constants within your class DATABASE_NAME
and DATABASE_VERSION
which are passed to the parent constructor meaning they are not required for the GroceryDBHelper
constructor. These variables are not required by a constructor as they will remain the same regardless of where this class is instanced.
When you pass these variables to the SQLiteOpenHelper
through the super()
invocation, SQLiteOpenHelper
is setting the private members which is why dbHelper.getDatabaseName();
is returning the correct result.
Upvotes: 1