• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections  RSS feed

 
Kanu Dialani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a small doubt. Suppose i have an ArrayList object which holds 10 objects of XYZ class. If i null out the ArrayList object, will it null of the 10 XYZ references.?

Thanx in advance.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No,
If you have reference of XYZ object held in some other variables or array or list etc you can still continue using the XYZ objects. Only reference held by the Arraylist will de-linked.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kanu:
Hello,
I have a small doubt. Suppose i have an ArrayList object which holds 10 objects of XYZ class. If i null out the ArrayList object, will it null of the 10 XYZ references.?

Thanx in advance.


Hi Kanu, your display name is against rules of JavaRanch. Please have a look at JavaRnch Naming Policy
and change your display name.

To answer your query: Only your ArrayList object reference will be made null, still you can use XYZ objects and they would not become null.
 
Rahul Gujarathi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an ArrayList object containing references to XYZ objects is set to null then all the objects i.e. ArrayList and XYZ objects would be eligible for garbage collection.

Only way to access the elements of the ArrayList is to create new references before destroying the old ones. As a result if we store a reference to the first object in the arraylist, we would be able to access that object even after setting ArrayList reference to null. Likewise if we want to access all objects, we'll have to create those many references.
[ February 27, 2006: Message edited by: Rahul Gujarathi ]
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Gujarathi:
If an ArrayList object containing references to XYZ objects is set to null then all the objects i.e. ArrayList and XYZ objects would be eligible for garbage collection.

Only way to access the elements of the ArrayList is to create new references before destroying the old ones. As a result if we store a reference to the first object in the arraylist, we would be able to access that object even after setting ArrayList reference to null. Likewise if we want to access all objects, we'll have to create those many references.

[ February 27, 2006: Message edited by: Rahul Gujarathi ]

Rahul are you sure? I guess you are wrong. Just small example code here:

import java.util.ArrayList;

public class test{
public static void main(String[] args)
{
String s1 = "s1";
String s2 = "s2";
ArrayList ar = new ArrayList();
ar.add(s1);
ar.add(s2);

System.out.println("length of array = "+ar.size());
ar = null;

System.out.println("s1 is = "+s1);

}

}

The above program will print sq even though we make ArrayList object ar null.

I am sorry if I understood your explanation in wrong.
 
Rahul Gujarathi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KJ you are absolutely right. The code that I had in mind was something like this...

new ArrayList();
ArrayList.add(new String("s1"))
ArrayList.add(new String("s2"))
ArrayList.add(new String("s3"))
..


In your code you already have a copy of a reference. So you'll able to access the object even if the reference within the ArrayList is destroyed.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rahul, for clarification.

May be our examples will give more details to Mr Kanu.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Kanu,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name. You can edit your name here.

Thank you for your prompt attention!

-Marc
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This raises another good question that If you have a circular reference of objects, but you no longer reference it from an execution thread, will this object be a potential candidate for garbage collection?

The answer is yes, because the referenced objects are not reachable from the main thread.
 
Rahul Gujarathi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AK,

Could you please elaborate on this?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!