Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Question on creating objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on creating objects" Watch "Question on creating objects" New topic
Author

Question on creating objects

Joe Baxter
Greenhorn

Joined: Dec 18, 2010
Posts: 2
Hi all - I am new to Java and new to this board. This is my first post. I am attempting to learn Java and I am having an issue with the interaction of Objects. I am working on the classic Elevator problem.
When I create an elevator Object within the constructor I then create floor objects and I am also going to create passenger objects. Is it bad design to create objects within another? It seems you must go all the way back to the first object to refer back to the correct object you created under it. I feel as if I am not understanding something basic. The elevator constuctor and floor object creation is below.

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
No, it is not bad design to 'construct' objects on a constructor. After all, that is what a constructor is for.

I think creating floor objects when creating an elevator makes sense. Floors, hopefully, will not get created or destroyed during the existence of the elevator. But creating passenger objects? Maybe a passenger object container but I would hope that an elevator exists before I get on it :-)
Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

Joe Baxter wrote:Hi all - I am new to Java and new to this board. This is my first post. I am attempting to learn Java and I am having an issue with the interaction of Objects. I am working on the classic Elevator ...


There is definately no issue with creating objects in a constructor. Refer to the Object Oriented Programming concepts of aggregation and composition.
Joe Baxter
Greenhorn

Joined: Dec 18, 2010
Posts: 2
Sorry if my question wasn't clear. I understand that you create an object in a constructor but does it make sense to construct objects of other types when you call a different constructor? Such as when you call an elevator constructor you then create floor and/or passenger objects. Then you must always refer to them as elevator.floor.... etc. Does that makse sense?

Thanks for the quick replies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
. . . and welcome to JavaRanch
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
What you describe is possible, and there may be program designs for which it makes perfect sense, but in the specific case of an elevator program, it may be silly. E.g., how does creating an elevator object add floors to the building? Likewise, why would destroying an elevator object remove floors?

Think or work through your design to see if what you envision makes sense. I think as you work it through to completion these kind of questions will answer themselves.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

Joe Baxter wrote:Sorry if my question wasn't clear. I understand that you create an object in a constructor but does it make sense to construct objects of other types when you call a different constructor? Such as when you call an elevator constructor you then create floor and/or passenger objects. Then you must always refer to them as elevator.floor.... etc. Does that makse sense?

Thanks for the quick replies.


Well that's is what I meant, though you can create an object anywhere, what matters is how you are holding the reference to that object. That all comes under the aggregation and the composition.

Your Elevator class could have Floor and Passenger variables to hold references of these type of objects which is aggregation and defines the HAS-A relationship. Alternatively, you may create the Floor and Passenger class into the Elevator class, known as inner classes which would be composition, in that case. Its all a matter of requirements of the design.

Regards,
Gaurav
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on creating objects