Lifetime of a static variable in Android

前端 未结 14 2220

when I declare and initialize a variable as static in my main activity and the activity gets destroyed. Can I still access the content of the variable?

For example t

14条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 10:30

    I believe I finally found you a reference -

    The garbage collector automatically cleans up unused objects. An object is unused if the program holds no more references to it. You can explicitly drop a reference by setting the variable holding the reference to null.

    https://docs.oracle.com/javase/tutorial/java/javaOO/summaryclasses.html

    To be clear, it is possible for static variables to remain initialized preventing the class to be properly garbage collected (aka a memory leak).

提交回复
热议问题