Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on creating objects

 
Joe Baxter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurav Sagar
Ranch Hand
Posts: 97
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic