Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java Ch.9 Constructors and Garbage Collection

 
Mike Wright
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I keep forgetting things like String and ArrayList are objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic