GoingTharn
GoingTharn

Reputation: 1131

Java object persistence question

I have an object class called BOM. It has a method, getChildItem() which returns an Item object.

Let's say I do this:

 BOM model = new BOM();
 Item child = model.getChildItem();
 ArrayList a = new ArrayList();

 a.add(child);
 model.close();

What happens? Does it:

Upvotes: 0

Views: 160

Answers (2)

Steve B.
Steve B.

Reputation: 57324

It's impossible to say what your close() method does. Possibly you're thinking of somehing like a Database resultSet or an inputStream where the values are unavailable after you've closed? That wouldn't be the case unless you've explicitly built your objects that way, they're not part of the core language.

From the context I think you mean "what happens when the parent object goes out of scope?" (i.e. becomes eligible for garbage collection)

What happens is this:

BOM model = new BOM();
 Item child = model.getChildItem(); 
 // you now have a handle to the child object. Presumably, so does model, but we don't care about that.
 ArrayList a = new ArrayList();
 a.add(child); 
//a now has a handle to child.
 model.close();
 // child is not eligible for garbage collection because a still has a handle to it.

Upvotes: 2

Mikezx6r
Mikezx6r

Reputation: 16905

Both child and model will be the same underlying object. close will be called on model, so any access to child that relies on it being open will now fail.

Hope that helps.

Upvotes: 0

Related Questions