This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes (re)assigning arrays? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "(re)assigning arrays?" Watch "(re)assigning arrays?" New topic
Author

(re)assigning arrays?

Ender Everett
Greenhorn

Joined: Mar 31, 2001
Posts: 19
I have a question about (re)assigning array reference variables.
If I create two arrays, I can reassign the reference variable name of one of them to the other array. No problem... I am curious, though, if I can ever reassign the reference variable to the original array, or if it is possible to assign a new reference variable to the now nameless array. If I can, how? It looks like the unassigned array is just floating out there in space waiting for the garbage collector to come by...
Here is code of a sample situation:

OK, So I reassigned the reference variable nums2. Now can I ever re-link to the array that was originally referenced through nums2 in any way? Is that whole object just lost now (as far as the program is concerned) and liable to be trashed at any time or... ? Just curious.
I have absolutely zero practical application for this at the present time... I just started studying Java about 10 days ago, and I am curious about the little tiny specifics of implementation.
To be totally up front, this is my first go at progamming ever, and this is all quite fascinating to me...
Anyway, thanks!


Don't sweat the petty stuff, just pet the sweaty stuff.
Tom McQueary
Greenhorn

Joined: Nov 24, 2000
Posts: 20
I believe you have correctly analyzed the situation. Since you assigned a new value to the reference variable that originally held the reference to the array of negative numbers, you lost your only way of accessing this array. Since the program cannot access this array, the array will eventually be garbage collected.
Ender Everett
Greenhorn

Joined: Mar 31, 2001
Posts: 19
Ah... OK. Thanks! Now I have to go find some practical application where it is useful to de-link and let an array slip off into the trash bin. ??
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The only way to save the new array from the garbage is to create another reference to it before you reassign the reference...



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Ender Everett
Greenhorn

Joined: Mar 31, 2001
Posts: 19
I thought about that.
But why would I ever want to do this? Is there ever any practical or realworld reason to do it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (re)assigning arrays?
 
Similar Threads
An object to hold several arrays
Sun Cirtification
Assigning array references
K&B unclear statement about arrays
Tricky Question