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(); 
    }
}
8
  • Is this the correct behavior you want? Commented Sep 27, 2012 at 3: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? Commented Sep 27, 2012 at 3:31
  • It seems you need cloning. clone list2 object and assign to list1. Now both object are null See the link stackoverflow.com/questions/715650/… Commented Sep 27, 2012 at 3:34
  • @TylerHolien: Yes, I tried it but when I do <code>list2 = new ArrayList<String>();</code>, list2 loses its reference to <code>list1</code>. Commented Sep 27, 2012 at 3:46
  • @DaoWen: I only want to initialize <code>list1</code>. I'm only planning to use <code>list2</code> to reference to list1. Commented Sep 27, 2012 at 3:49

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am sure the: List<String> list1 = null; List<String> list2 = list1; Code compiles.
@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.
@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 - 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.
@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.
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 

2 Comments

Yes, list2 would still be null but won't it have an access to list1 in that way?
@Arci - No, it will not. Java does not have variable aliasing. See my answer.
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.

5 Comments

list2.add("hi") <--- causes NullPointerException if list1 = null;
@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?
@gtgaxiola Of course, the question deals with instantiating list2 and he even says he knows you can instantiate list1 using new ArrayList.
@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.
list2 = new ArrayList(list1); will throw NullPointerException
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.