Hi,
I am working my way through Head First
Java Second Ed. and I am unclear on a point.
I understand one can create objects, and those objects can generate additional objects. In my code example, an art colony makes artists, and the artists make art pieces. Setting the reference variable to an object-which-made-other-objects to null eliminates that object and the objects it made, right? What if one wants to preserve those 'sub-objects' while setting the Artist to null? That is, eliminate the artist, but not their art pieces. While goofing around with this issue, I discovered I could not get a System.out.println() statement in ArtColony to print the artTitle for the piece made by the artist Kim. I was, however, able to print the artistOne.name and the artistOne.age. To get the newestPiece.artTitle to print I had to create the statement in the Artist class. Somehow this is all connected...
This all started as I was musing over objects creating objects which create objects. How does one reference the methods and attributes of these "sub objects"?. I was supposing that in programming, say, a city, you'd have cars, the cars would generate engines, the engines would generate engine parts, etc. Then, to ditch all these objects when one wanted to, one could just set the reference variable for the car instance to null, and not have to worry about how to get rid of the sub objects. However, how do you find out what spark plugs attributes exist from the 'main' method, while the car still exists? How do send everything BUT the spark plugs to Garbage Collection? What am I missing?
Feel free to correct the code, my question, or my description of the problem, even in minor ways.
Thanks...
Patrick