jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Creating objects question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating objects question" Watch "Creating objects question" New topic

Creating objects question

David Pantale
Ranch Hand

Joined: Mar 16, 2010
Posts: 32
I have an object orientation question, which I'm not sure there really is a good general answer to (and which nobody may care about!). But here goes, when creating an object that will be contained by another object, what is the best way to create the inner object? Should you create the inner object outside the containing object and use a setObject method to place it in the containing object, or create it from within the container object(after passing in any data necessary for the creation)? Obviously this could often be situation dependent, but does anyone have an opinion as to a general preferred method?

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4637

Like you said, it depends. If the relation between the objects is a simple association or an aggregation, then I generally prefer to create the second object externally. However, in a composition it's better to create it inside the first object.

Classes should be easy to use. If a class can do all the work by creating its own internals, it should definitely do so.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link: http://aspose.com/file-tools
subject: Creating objects question
It's not a secret anymore!