0

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(); 
    }
}
phant0m
  • 16,595
  • 5
  • 50
  • 82
Arci
  • 6,647
  • 20
  • 70
  • 98
  • Is this the correct behavior you want? – gtgaxiola Sep 27 '12 at 03:28
  • You want to initialize `list1` using `list2` using the case above? So you want to make a cyclic definition? Or did you actually mean _initialize `list2` using `list1`_? – DaoWen Sep 27 '12 at 03:31
  • It seems you need cloning. clone list2 object and assign to list1. Now both object are null See the link http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents – Elbek Sep 27 '12 at 03:34
  • @TylerHolien: Yes, I tried it but when I do list2 = new ArrayList();, list2 loses its reference to list1. – Arci Sep 27 '12 at 03:46
  • @DaoWen: I only want to initialize list1. I'm only planning to use list2 to reference to list1. – Arci Sep 27 '12 at 03:49
  • @elber: I can't assign a value directly to list1 since I don't have access to it. – Arci Sep 27 '12 at 03:50
  • Who wrote `Class1` to have no setter? It seems like a supremely useless class. The only way to make it useful is to change the class itself. If you can't change the class, the only thing you can do is declare a derived class that returns something sensible from `getList1()`. – Ted Hopp Sep 27 '12 at 04:30
  • What are you actually trying to do here? Do you have access to the source code for `Class1`? What purpose does `list1` serve if it's always `null` and you have no way to change it? – DaoWen Sep 27 '12 at 04:31

4 Answers4

2

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I am sure the: `List list1 = null; List list2 = list1;` Code compiles. – Elbek Sep 27 '12 at 03:36
  • 1
    @elber - Yes, but OP is asking if _after that_, whether `list2` can be used to initialize `list1` (presumably to something other than `null`). Plus, your example does not initialize `list1` using `list2`, which is what OP wants. – Ted Hopp Sep 27 '12 at 03:36
  • @TedHopp: Thanks! I think you're the only one who understood my question. I'm looking for a way to initialize a variable with the use of pointers. I know it's something that can be done in C so I'm no so sure if there's a way to do it in Java. Is there any other way to initialize list1 if I don't have a direct access on it? However, the default value of list1 is null. – Arci Sep 27 '12 at 03:55
  • 1
    @Arci - Sorry. In Java, the only way to affect the value stored in a variable is to assign to the variable. This is true for reference types as well as primitives. (With reference types, you can of course modify the object that is referenced, as long as you have a copy of the reference--say, by passing a parameter or by assigning the reference to another reference variable.) Java does not have pointers, especially pointers to variables. – Ted Hopp Sep 27 '12 at 04:09
  • @TedHopp: Oh. Thank you! :) I thought objects can act similar to pointers. Maybe, I'll just tweak the other class. I'm avoiding to tweak it as much as possible but I guess I don't have a choice. – Arci Sep 27 '12 at 05:31
1

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 
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
0

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.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • list2.add("hi") <--- causes NullPointerException if `list1 = null;` – gtgaxiola Sep 27 '12 at 03:32
  • @Jesus Ramos: If I do that, list2 is already a new object. Is it possible to just make a reference to list1 and modify it using list2? – Arci Sep 27 '12 at 03:34
  • @gtgaxiola Of course, the question deals with instantiating list2 and he even says he knows you can instantiate list1 using new ArrayList. – Jesus Ramos Sep 27 '12 at 03:34
  • @Arci Yes Java uses references so if you say list2 = list1 they both point to the same list and any modifications made on list2 will be present in list1. – Jesus Ramos Sep 27 '12 at 03:35
  • `list2 = new ArrayList(list1);` will throw NullPointerException – Elbek Sep 27 '12 at 03:38
0

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.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72