0

I have read it everywhere that Java stores Object references in array. Even i demonstrated it myself. But then i changed the state of the object, means i changed the values of attributes and saved in array, and i can retrieve multiple state of same object. If Array saves only references then how do references holds the states. For example:

class Test{
String id;
}
Test[] testArr = new Test[2];
test = new Test();
test.id = "ABC"
testArr.add(test)
test.id = "XYZ"
testArr.add(test)

now in case above if we would storing only references then second assignment would have overwritten id value of test object and both entry in array would have same value of id, but this is not the case and we can retrieve the id values ABC and XYZ. I am confused!

Ankush G
  • 989
  • 9
  • 14
  • 4
    this doesn´t even compile... you are invoking `add` on a normal array. and yeah if this would be done in a correct way you would have the same instance of `Test` twice in the array, with the `id` value of `XYZ`. In addition, no arrays don´t only store references, they store the values of the reference, or for primitives the value itself. [refer to Is Java “pass-by-reference” or “pass-by-value”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – SomeJavaGuy Dec 04 '15 at 09:14

2 Answers2

0

To answer your question. Yes. All variables are simply references to the location of an object, not just an ArrayList. Writing MyObject obj = new MyObject(); creates a new object however the variable obj is just a reference to the location of that object in the Heap (aka memory).

An ArrayList (without looking at its actual implementation) simply stores each reference to the location of an object as an index of the ArrayList rather than a unique variable.

In a bit more detail: You need to understand what each part of creating an object does. Try to imagine it in this way:

Each object is located in a memory address that takes up the number of bytes all its fields use. Lets imagine we have an object called MyObject that takes up 100 bytes. When we create an Object using the new keyword (ie. new MyObject()) it is stored in a memory location in the heap (which is an area of memory set aside for your program to use as dynamic memory allocation). Let us say that when this object is created it takes up memory space 1000 (up to 1100 because it uses 100 bytes of memory). So:

|MyObject| <-- this is a visualization of memory space
1000

The when we write MyObject obj it sets aside memory in the stack (which is used for static memory allocation) and this will hold the location of the object. So it may hold the reference to the location of the object in its own location which we will pretend is labeled 4

|______| <- empty memory location because it hasn't been assigned yet.
4

When we put the 2 instruction together and write MyObject obj = new MyObject() it puts the address of the object into the memory location of the variable so we end up with:

|MyObject| <-- location of actual object
1000

|1000| <-- location of variable which a reference to the location of the object
4

yitzih
  • 3,018
  • 3
  • 27
  • 44
0

Take a look at this code:

class Test {
    String id = "A";
}

public class Main {

    public static void main(String[] args) {
        ArrayList<Test> list = new ArrayList<Test>();

        // Adding some Test Object to the list.
        Test foo = new Test();
        list.add(foo);
        System.out.println("Value of foo id: " + foo.id);

        // Retrieving the Object from the list & changing the value
        Test bar = list.get(0);
        bar.id = "B";

        System.out.println("Value of foo id: " + foo.id);
    }
}

output:

Value of foo id: A
Value of foo id: B

As you can see the arraylist only holds references. If you retrieve the Object from the list and change something in it, the original Object will be changed too

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51