Android static variables are lost

前端 未结 3 1736
粉色の甜心
粉色の甜心 2021-01-19 01:46

I am using a class with only static variables to store all constants and run-time properties. But, recently I have seen instances when I\'m referencing these variables, I ge

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 02:08

    Please note the following in reference to above question:

    1. Garbage collector removes only unreferenced objects. Static variables will lose their values only when they are unloaded from JVM during run-time.

    2. Initialization happens when app is launched via activities. If there are any services/broadcast receivers that are accessing methods within other classes and use these ids, and the app is not yet running by that time, the uninitialized values are referenced as default value. In case of string - it is null.

    3. Nothing to do with low-end processors, easy to blame them often but JVM is powerful enough.

提交回复
热议问题