Question

I've been following this tutorial for creating a database and have one question. It says "To access your database, instantiate your subclass of SQLiteOpenHelper: FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());". But how can I instantiate my FeedReaderDbHelper class in another class (in a service in my case) without instantiating the whole FeedReaderContract class, because it says I should avoid instantiating the former class? In my service I am doing this:

FeedReaderContract.FeedReaderDbHelper mDbHelper = new FeedReaderContract().new FeedReaderDbHelper(mContext);

But the tutorial says it's forbidden.

Was it helpful?

Solution

If your inner class is associated to an instance of your outer class, you wouldn't be able to do that. IOW, if your inner class is not a static inner class, then it really doesn't make sense to create your inner class alone, right?

On the other hand, you can change your inner class to a static inner class. But you have to be careful if this is what you need since you are basically saying that the static inner class is not dependent on any one instance of the outer class.

Check this out: https://stackoverflow.com/a/70358/2231632

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top