From my Java understanding, static blocks should be executed before ANY other function.
I have a static block that calls a static function, both in class MyClass.java, that uses SharedPreferences object.
MyClass is used by MyApplication (Extends Application), somewhere in the onCreateMethod.
static {
doSomethingStaticFirst();
}
private static void doSomethingStaticFirst() {
//Log(DO_SOMETHING_STATIC_FIRST)
UserPreferences userPreferences = new UserPreferences(MyApplication.getInstance().getApplicationContext());
}
However I am seeing the following logs in order:
- MyApplication onCreate method
- DatabaseManager onUpdate (in case DB version increased)
- DO_SOMETHING_STATIC_FIRST
Is there a particular reason why static blocks are not executed first? Will MyApplication onCreate
method will be executed first over all?