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

查看:170
本文介绍了从另一个类访问静态的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在下面的class.But它总是空的。

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是人口动态,这意味着我不能用静态{..} block.How我可以访问静态的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.

任何帮助是AP preciated。

Any help on this is appreciated.

推荐答案

一个静态字段做的的保留其在整个程序执行价值,但只在一个执行。静态字段的目的是使提供的值,而无需获得任何类的一个实例。只有一个主要的方法将被隐式调用所以要么名单将被填充和遗忘,或空列表将与其他入口点读取。

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.

究其原因静态{} 块的工作是检索列表之前他们得到的某个时候运行的无论入口点

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

下面将为此工作:

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天全站免登陆