wood burning stoves*
The moose likes Beginning Java and the fly likes Head First Java Ch.9 Constructors and Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Ch.9 Constructors and Garbage Collection" Watch "Head First Java Ch.9 Constructors and Garbage Collection" New topic
Author

Head First Java Ch.9 Constructors and Garbage Collection

Mike Wright
Greenhorn

Joined: Feb 24, 2006
Posts: 2
Hello,

This forum was recommended in Head First Java so I thought I'd give it a shot. I was trying to do the 5 minute mystery at the end of the Garbage Collection chapter and I am confused. I am hoping someone can help me to better understand.

The puzzle revolves around a program which is creating too many of one type of object due to the way the constructors are written. I understand how the number of objects created is reached. My problem is I don't understand how the objects are surviving to be counted in the end. To me it seems they should be destroyed right after they are made. Below is the code:

import java.util.*;
class V2Radiator {
V2Radiator(ArrayList list) {
for(int x = 0; x < 5; x++) {
list.add(new SimUnit("V2Radiator"));
}
}
}

class V3Radiator extends V2Radiator {
V3Radiator(ArrayList lglist) {
super(lglist);
for(int g = 0; g < 5; g++) {
lglist.add(new SimUnit("V3Radiator"));
}
}
}

class RetentionBot {
RetentionBot(ArrayList rlist) {
rlist.add(new SimUnit("Retention"));
}
}

public class TestLifeSupportSim {
public static void main(String[] args) {
ArrayList aList = new ArrayList();
V2Radiator v2 = new V2Radiator(aList);
V3Radiator v3 = new V3Radiator(aList);
for(int z = 0; z < 20; z++) {
RetentionBot ret = new RetentionBot(aList);
}
}
}

class SimUnit {
String botType;
SimUnit(String type) {
botType = type;
}

int powerUse() {
if("Retention".equals(botType)) {
return 2;
}
else {
return 4;
}
}
}

The way I am looking at this is a copy of the ArrayList aList from Main is passed to the ArrayList list parameter in the constructor for an object like V2Radiator. The ArrayList list has 5 SimUnit objects added to it. The constructor ends its run and here is where I am confused.

If I understand correctly there is no connection between ArrayList list and aList because it was a copy of aList that was passed. Therefore, aList in main should not have any SimUnit objects, only list does. There is no instance variable for V2Radiator and list is a local variable of the constructor method. Shouldn't list then be blown off the stack once the constructor is done running? Wouldn't there need to be an instance variable of type ArrayList that is updated by the constructor in order for the ArrayList of SimUnit objects to survive?

I hope the above was understandable. Any insight anyone can provide is greatly appreciated.

Thanks,
Mike Wright
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
ArrayList list and ArrayList aList are in fact the same object. Java only passes pointers to the existing list. Therefore, list has five elements added after V2Radiator, ten after V3Radiator, and so on.
Michael Hall
Greenhorn

Joined: Dec 17, 2005
Posts: 21
Shouldn't you have said a reference to alist is passed - not a pointer? Please explain as I might be confused - thanks
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
As far as Java is concerned, pointers are references and references are pointers. Only in languages like C/C++ do they differ, and even then only slightly. For instance:



The two perform exactly the same function (in fact when compiled to machine code, they should be identical in operation), and only their invocation is different. In Java, this distinction is blurred since only one form exists, and without an existing comparison to the other, there's really no way to know whether it's a pointer or a reference.

It's all just terminology anyway; it doesn't matter in the long run.
Mike Wright
Greenhorn

Joined: Feb 24, 2006
Posts: 2
Thanks for the help. I keep forgetting things like String and ArrayList are objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java Ch.9 Constructors and Garbage Collection
 
Similar Threads
call by value/reference
reference variable
How to discard objects stored in an ArrayList
compilation error
Head First Java Five Minute Mystery p. 268