• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Objects Generated by Objects

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Patrick Jones
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sweet! I think I get it now!
 
Patrick Jones
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool...this makes sense...I'll try it now...

Patrick
 
reply
    Bookmark Topic Watch Topic
  • New Topic