从另一个类访问的静态 ArrayList 始终为空 [英] Static ArrayList accessed from another class is always empty

查看:37
本文介绍了从另一个类访问的静态 ArrayList 始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程

import java.util.ArrayList;
import java.util.List;

public class TestStaticArrayList {

    public static List<String> numberList = new ArrayList<String>();

    public static List<String> getArrayValues(){
    return numberList;
    }

    public static void populateArray() {
    numberList.add("1");
    numberList.add("2");
    numberList.add("3");        
    }
}

上面类中的静态 ArrayList 是通过下面类的调用动态填充的.

The static ArrayList in the above class is populated dynamically from a call from below class.

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TestStaticInvokationClass {

    public static void main(String[] args) throws Exception {

    Class staticKlass = Class
        .forName("com.sathish.test.TestStaticArrayList");
    staticKlass.getMethod("populateArray", null).invoke(null, null);
    }
}

我正在尝试访问下面类中的静态 ArrayList.但它始终为空.

I am trying to access the Static ArrayList in the below class.But its always empty.

public class StaticArrayListAccessTest {

    public static void main(String[] args) throws Exception {   
    System.out.println(TestStaticArrayList.getArrayValues());

    }
}

如果我使用

static {
...
}

填充 Arraylist 它工作正常.我有一个用例,其中 ArrayList 是动态填充的,这意味着我不能使用静态 {..} 块.如何在这种情况下访问静态 ArrayList.

to populate the Arraylist it works fine. I have an use case where the ArrayList is populated dynamically which means I cannot use a static {..} block.How can I access the Static ArrayList in this scenario.

对此的任何帮助表示赞赏.

Any help on this is appreciated.

推荐答案

静态字段在程序执行中保留其值,而仅在一次执行中保留其值.静态字段的目的是使值可用而无需获取任何类的实例.只有一个 main 方法将被隐式调用,因此列表将被填充和遗忘,或者将从另一个入口点读取空列表.

A static field does not retain its value across program executions, but only within one execution. The purpose of a static field is to make a value available without having to acquire an instance of any class. Only one main method will be called implicitly so either the list will be populated and forgotten, or the empty list will be read from the other entry point.

static { } 阻塞工作的原因是它们在检索列表之前的某个时间运行,无论入口点.

The reason static { } blocks work is that they get run sometime before the list is retrieved, no matter the entry point.

出于这个原因,以下将起作用:

The following will work for this reason:

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TestStaticInvokationClass {

    public static void main(String[] args) throws Exception {

    Class staticKlass = Class
        .forName("com.sathish.test.TestStaticArrayList");
    staticKlass.getMethod("populateArray", null).invoke(null, null);
    System.out.println(TestStaticArrayList.getArrayValues());
    }
}

它将填充 ArrayList,然后尝试在同一执行中读取它.

It will populate the ArrayList and then try to read it in the same execution.

无论如何,您不需要使用反射,因为在这种情况下它更慢、更危险且没有根据.只需执行以下操作:

Anyway, you need not use reflection, as it's slower, riskier, and unwarranted in this situation. Simply do as follows:

TestStaticArrayList.populateArray();

这篇关于从另一个类访问的静态 ArrayList 始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆