This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Regarding Objects Generated by Objects 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 "Regarding Objects Generated by Objects" Watch "Regarding Objects Generated by Objects" New topic

Regarding Objects Generated by Objects

Patrick Jones

Joined: May 26, 2009
Posts: 20

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 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.



Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

In your code, artists create a piece of art and throw it in the trash immediately. They would need to store it:
Of course this is still not 100% functional. With this code, the artist keeps his art in his gallery but when he dies, he takes all of it to his grave. You'll need to publish the art pieces. For instance:
The art colony can now get the art piece the artist just created. It should be stored like in the artist code I've given before or the art colony will discard the art piece (similar to giving it back to the artist).

Of course my analogy is not 100% correct because if the art colony stores the art piece, both the art colony and the artist have a reference to the same art piece. But you get the idea I think.

How To Ask Questions How To Answer Questions
Patrick Jones

Joined: May 26, 2009
Posts: 20
Sweet! I think I get it now!
Patrick Jones

Joined: May 26, 2009
Posts: 20
Cool...this makes sense...I'll try it now...

I agree. Here's the link:
subject: Regarding Objects Generated by Objects
Similar Threads
variable in place of object?
Help with project using graphics
TreeSet not allowing the addition of a unique object, yet the equals() method is not being called