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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating objects question" Watch "Creating objects question" New topic
Author

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?

Thanks
Dave
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3366
    
    9
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: http://aspose.com/file-tools
 
subject: Creating objects question
 
Similar Threads
Declare non-static "local class" within static method
Inner classes
Inner Class explaination?
Changing the variable in method scope inside the method ?