Originally posted by Nabila Mohammad:
...Also I did not understand
HeavyItem list = createList("X");
list = createList("Y");
itemA = new HeavyItem(itemID, null);...
It would be difficult to grasp garbage collection without understanding object creation, because garbage collection is all about "unreachable" objects that can be cleared from memory.
new HeavyItem(itemID, null) creates a new instance of HeavyItem by calling a HeavyItem constructor that takes arguments of a
String and a HeavyItem. In this case, the String is referenced by itemID, and the HeavyItem is a null reference.
createList is a method that takes a String argument and returns a reference to a HeavyItem. In fact, the method creates
three new instances of HeavyItem and returns a reference to one of these. But this HeavyItem HAS-A HeavyItem, which in turn HAS-Another HeavyItem.