• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Head First Java Ch.9 Constructors and Garbage Collection

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help. I keep forgetting things like String and ArrayList are objects.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic