java - Android 中的静态场生命周期

标签 java android static garbage-collection

小问题,但我无法在文档中找到答案。

Android 应用中静态字段的生命周期是多长? 什么时候初始化,什么时候销毁? final 属性是否修改了它的生命周期?私有(private)/公共(public)呢?只要 Application 实例存在,它们就存在吗?

举两个例子:

public class DemoClass {
   static int one = 1;
   static int three = DemoActivity.two + one;
}

public class DemoActivity extends Activity {
     public static int two = DemoClass.one + DemoClass.one;
     private static final int four;
     public static int five;

     public void onCreate(Bundle b) {
          four = two + two;
          five = DemoClass.three  + DemoClass.one + DemoClass.one;

     }

}

编辑:还有静态字典呢?

例如,

public class AnotherDemoActivity extends Activity {
public static ArrayList<String> strings = new ArrayList<String>();

@Override public void onCreate(Bundle b) {
   strings.add("test");
   strings.add(new String("another test");
   strings.add(new DemoClass());
}

“字符串”元素会存在多长时间?

最佳答案

When is it initialized and when is it destroyed?

它在加载声明它的类时被初始化,它将一直存在到您的应用程序进程结束。

Does the final attribute modify its lifecycle?

没有

What about private/public?

没有

Do they live as long as the Application instance is alive?

关于java - Android 中的静态场生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12692106/

相关文章:

java - 关于简化与循环迭代相关的代码的问题

java - 使用 Chaquopy 将多个列表从 Python 返回到 Java

java - 如何不对 ChromeDriver 路径进行硬编码?

android - Mockito 2 for Android Instrumentation 测试 : Could not initialize plugin: interface org. mockito.plugins.MockMaker

android - 如何从选项卡下的组 Activity 下的 Activity 中启动 Activity 结果?

java - XML 到 JSON 可以选择包含一个元素的数组

c++ - 在 C++ 中,单例(静态实例)一旦程序退出就会被销毁的原因是什么

java - 使用基于对象的 run() 方法而不是静态 main 有什么优势吗?

asp.net - 在 ASP.Net 应用程序中使用全局变量的最佳方法是什么?

java - 在 Android 中搜索数据库