Arci
Arci

Reputation: 6819

How to instantiate a variable using another variable?

How do I instantiate a variable via another variable which is referencing it? For example:

List<String> list1 = null;
List<String> list2 = list1;

How do I instantiate list1 using list2? I know that list1 can easily be instantiated with list1 = new ArrayList();. But what I want to know is if it is possible to instantiate list1 using list2 given the case above?

Just for clarification: What I want to achieve is to have an access to list1. I have a class which contains list1 and I need to modify the value of list1. unfortunately, that class did not provide a setter for list1 and list1 is still null.

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(); 
    }
}

Upvotes: 0

Views: 537

Answers (4)

Amit Deshpande
Amit Deshpande

Reputation: 19185

You can extends that class and provide your own implementation of getList1() method by extending it.

public class Class2 extends class1
{
    private List<String> list1 = new ArrayList<String>();
    public List getList1()
    {
    return list1; //value of list1 is null.
    }
}

The reason above will not work is your list1 contains null reference and java does not provide a way to set reference to object other than new or newInstance() besides some exception String, primitives but compiler does the same for them also.

Upvotes: 0

Jesus Ramos
Jesus Ramos

Reputation: 23268

Collections all have a constructor that takes a Collection as a parameter that makes a copy of the collection in the same order as was returned by the iterator of that collection (in the case of ArrayList).

list2 = new ArrayList(list1); would work.

It looks like I misunderstood what the asker wanted (I assumed he meant just any other list and not a list that pointed to null).

In that case list2 will always be null no matter what is assigned to list1.

Upvotes: 0

Caffeinated
Caffeinated

Reputation: 12484

In your example, you can't. Since list1 is null, you won't get anywhere by the line:

List<String> list2 = list1;

list2 would still be null

Please peruse the List API page. Also, if you try this out in the DrJava interactions pane you'll see it better. If you tried to add a String after those 2 lines, expect to get a

java.lang.NullPointerException 

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234795

I think that what you're asking is if there is some indirect way of initializing list1 by using list2. In Java, the only way to change the value of list1 is to assign something to it.

In Java, list2 will be null after your code executes. Unlike C++ (or C), Java does not have references to references (or pointers to pointers), there is no way that list2 can be used to indirectly initialize list1.

Upvotes: 2

Related Questions