Tam H. Doan
1 min readFeb 3, 2020

--

Hi Ali, I think it depends on which virtual machine version that your Java code is running on. Your Stack Overflow link use reference from the official JLS, not Android-specific.

I just did a test like this:

public class MyView {
public static final String TAG = getSimpleNameFake();
static String getSimpleNameFake() {
Log.e("MyView", "getSimpleName");
return "";
}
}

I disabled Proguard/R8 then Run the app on my phone (Android 7) to see whether my TAG variable is initialized or not. But unfortunately, it’s not.

I use getSimpleNameFake() method because I cannot write a Log inside getSimpleName() — which belongs to a Class.

So I still think the static constants of a Class only be initialized when this Class is accessed the first time (after the class is loaded by ClassLoader, of course). Because long app startup time is a critical problem on Android, I think Google never implements a VM that init all static variables at app startup.

Maybe I’m doing something wrong with the test, so what do you think? Do you have any Android-specific reference or real-life test for this case?

--

--

Tam H. Doan
Tam H. Doan

Written by Tam H. Doan

Software Engineer, also a Writer and Runner

No responses yet