aspose file tools
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 The Software Craftsman this week in the Agile 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: 3679

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.
I agree. Here's the link:
subject: Creating objects question