Java Reference
In-Depth Information
However, when this method executes the following:
anIntArray[0]++;
It will, of course, point to the same address in memory, as you have created a copy of the piece of
paper, but both contain the same address. This is why the changes are reflected in the member vari-
able, even though your second piece of paper ( anIntArray ) is thrown away once the method exits.
Figure 4-6 depicts this in the simplified view you've been following.
Memory Address
#000023
Memory Address
#000025
Memory Address
#000025
Test t
int[] t.array
anintArray
(A copy is made to pass
as method argument, but
contains same memory
address reference.)
23
24
25
26
27
28
t object
(general info, such
as length of
following variable
addresses)
array var
=
000025
array[0]
= 1
array[1]
= 2
array[2]
= 3
anintArray var
= 000025
figure 4-6  
Now let's take a look at what happens next:
Test.changeIntArray(t.array);
Again, there is the same reasoning. You do not pass the t.array piece of paper itself, but again
make a copy to pass to the changeIntArray method, which is named anIntArray . However, this
method calls:
anIntArray = new int[] {100,200,300};
Meaning that you create a new object and scribble down its address on the copied anIntArray
sheet of paper, overriding the old one, which is different from the one written in t.array . This
means that all the changes you then make to anIntArray will not be reflected in the member vari-
able (as the addresses differ). Even more, the object stored in the location referred to by anIntAr-
ray will cease to exist when the method exits, as the piece of paper is discarded, and Java will
detect that the object living at that address can no longer be accessed through any variable, as
shown in Figure 4-7.
Search WWH ::




Custom Search