Why database name and version must be static? - android

Ok here is same question why database name must be static?
but i know already that why we should declare database name static & final.
And i tried to give non static database name into constructor of SQLiteOpenHelper but end up with an error the field can't be quoted from the static context
I want to know/find the source code or particular line where it was decided to make those ( database & version ) static.
I looked already https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/database/sqlite/SQLiteOpenHelper.java but couldn't find.

It's about Java, not about Android sqlite.
When initialising an object instance, constructor is invoked before initialising member fields. The database name is a constructor argument and needs to be initialised when invoking the constructor. When it's static, it's not an instance member but a class member that is initialised earlier when the class is first accessed.
See Java order of Initialization and Instantiation

Related

Why is Android Studio complaining about getApplicationContext()? [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 3 years ago.
Here is my method:
public Cursor rawQuery(String sql, String[] selectionArgs) {
try {
return m_db.rawQuery(sql, selectionArgs);
} catch (SQLiteException e) {
reportException(Context.getApplicationContext(), e);
return null;
}
}
Android Studio (3.5.3) complains saying
Non-static method getApplicationContext() cannot be referenced from a static context.
I don't see any static context here. rawQuery is a perfectly good class method of a class which is a wrapper around SQLiteDatabase to save me having to check for exceptions on every call. The exception checks are needed because it's accessing a public database and some other process may have modified it in such a way that my operation fails (for example by dropping one of my tables). It is (currently) only created and its methods called from an Activity. Of course I could pass the calling activity's context, but this is clumsy since it isn't needed most of the time and IMHO it's very poor programming style to include extra arguments in the methods of a wrapper class.
reportException does what its name says and writes a log file or displays a Notification or a Toast, depending on circumstances, and all of these need a context.
I've seen suggestions on the net to create a static instance of a subclass of Application to cache the application context. As commenters have pointed out, this doesn't always work if you need the application context in a class constructor (or anything which is called from one), but I don't expect to do this. My wrapper class is created as needed when I want to access the database. However I'm not sure if tha Application subclassing trick works if I open a database in a background server which may get kicked out of memory when not active and later restarted by the OS. It may be that the only solution is to cache the creator's context in the constructor of the wrapper class: this only requires passing the context once. However I don't much like the idea of keeping a copy of the passed context: it looks inelegant and a potential problem with garbage collection since I have to take care not to use the cached context when creating anything persistent..
However I still don't see Android Studio's justification for complaining in the case shown. I tried removing all the calls to rawQuery and it still complains, so it isn't walking the call tree to look for a non-static context. It looks as if it may be complaining if getApplicationContext is used in any class which isn't a subclass of Activity, which certainly isn't justified.
I don't see any static context here.
The "static context" referred to by the error message is the way you are calling the method: Context.getApplicationContext(). Since you are using the Context class name, this counts as a "static context". You need a Context instance in order to call getApplicationContext().
Of course I could pass the calling activity's context, but this is clumsy since it isn't needed most of the time and IMHO it's very poor programming style to include extra arguments in the methods of a wrapper class.
Yes, I agree that you should keep your argument list as trimmed down as possible. You say that this method is a wrapper around SQLiteOpenHelper which requires a Context as one of its constructor parameters. So presumably your own constructor takes a Context to pass to the wrapped SQLiteOpenHelper instance. One solution is to keep that Context as a field in your class. Then you can just use this.context.

Android DB controller error

Hello everyone :) can you explain why I get this and tell me what how I have to change my code. Waiting for the answers. Have a nice day:)
controller
cannot be used in a static method. You need to make the controller object static to be able to use in a static method.
It appears that you are attempting to use a non-static member field inside a static method. This is not allowed since you need an instance of the class to access the member field. You have two choices:
Make the member field a static class variable by adding the static modifier to its declaration.
Remove the static modifier from the method to make it non-static.
You should definitely prefer the second solution over the first. As a general rule of thumb, all methods and variables should be non-static unless you have a very good, specific reason to make them static. One common use of static variables is for final constants. You also might find a situation where all instances of a class share a single value. Note that these are the exception, not the rule.

SqliteDatabase.getDatabaseName for API level 10

I want to get the name of an opened database, for which I have a reference via a SqliteOpenHelper. No problem with API level 14 (getDatabasename). But I need it to work with API level 10 (hard requirement--this is for a class I'm taking).
The only idea I've come up with so far is storing the database name myself for future use--either in the class in which I need it or as a member of a subclass of SqliteOpenHelper. Is there a better way? Thanks.
getDatabaseName() returns the exact same name that you provided in the SqliteOpenHelper constructor, so the best is probably to store it in a member variable of a subclass. It will be more flexible if later you have another project where you have the same problem.
but dont you already have the database name since your opening it and passing it to the constructor of SqliteOpenHelper ? So just subclass SqliteOpenHelper and create the method getDatabaseName() yourself and let it do what you want.
this seems to be more about object oriented principals. Let me know if you need code or if im mistaken.
I grepped the code . the function your looking for simply stores the variable you set in the constructor:
public String getDatabaseName() {
return mName;
}
so again make your own
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.1_r1.2/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper.getDatabaseName%28%29

ActiveAndroid- unable to create Tables when initialized explicitly

In my application rather than following the regular hierarchy of the ActiveAndorid,
I'm explicitly calling with the .initialize() method by passing the getApplicationContext.
However, the TableInfo returned doesnt have any Tables from the model class that I'm creating.
I tried debug their code and it seems an issue with the classloader they are using.
My code is:
ActiveAndroid.initialize(getApplicationContext());
TestModel model=new TestModel();
model.value="hello";
model.save();
You should not use getApplicationContext. It might not return the context of your application. The naming of getApplicationContext is misleading.
More info here: getApplication() vs. getApplicationContext()

2 BroadcastReceivers and 1 static variable

I have 2 BroadcastReceivers in my android application. They are in the same package.
If in the onReceive() method, they both read/write a static class variable (in a separate Util class). Does android create 1 copy of that static class variable or 2 (1 for each receiver)?
And what do I need to do to make sure they are accessing the static class variable not corrupting the data?
There will only be one instance of the static variable. From http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html:
A class variable is any field declared
with the static modifier; this tells
the compiler that there is exactly one
copy of this variable in existence,
regardless of how many times the class
has been instantiated.
To avoid problems, you will have to make sure the static variable is thread safe. Some data structures, like Vector, are already thread safe, so you would not have to do anything further. Otherwise, you may have to use the synchronized keyword or something from the java.util.concurrent.locks package.
Don't use static variables, make the class Singleton.
"And what do I need to do to make sure they are accessing the static class variable not corrupting the data?" - add synchronized to getters/setters

Categories

Resources