如何通过引用它的另一个变量来实例化一个变量?例如:
List<String> list1 = null;
List<String> list2 = list1;
如何list1使用 list2 进行实例化?我知道list1可以很容易地用list1 = new ArrayList();. 但我想知道的是,在上述情况下,是否可以使用 list2 实例化 list1?
只是为了澄清:我想要实现的是可以访问 list1。我有一个包含的类,list1我需要修改list1. 不幸的是,该类没有提供 setterlist1并且list1仍然为空。
public class Class1
{
    private List<String> list1 = null;
    public List getList1()
    {
        return list1; //value of list1 is null.
    }
}
public class Class2
{
    public static void main(String[] args)
    {
        Class1 class1 = new Class1();
        // then I need to set the value of list1.
        // However, list1 did not provide a setter method
        // so my only way to access it is using a reference.
        // with the code below I am assuming that I can
        // create a reference to list1 and set its value.
        // How do I set the value of list1?
        List<String> list2 = class1.getList1(); 
    }
}