This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
It means that you're passing a reference to another object, so that you can create the new object using some information from the other one.
The example you've got there is just one case where it's useful. That's what's known as a "copy-constructor", which allows you to create a new Box object with the same settings as an existing Box object. So you pass a reference to a Box into the constructor, which can then initialise the new object accordingly.
(Incidentally, that code won't compile, as there are bits missing. Specifically, it would need the additional constructors that are being used on lines 16, 17 and 18)
Joined: Sep 14, 2009
What is the difference between "passing objects to methods" and "passing objects to constructors"? When should we use "passing objects to constructors"?. When writing a code when can we use "passing objects to constructors"?
Joy Vergis wrote:What is the difference between "passing objects to methods" and "passing objects to constructors"?
Nothing at all. In terms of passing the object they work in the same way. A constructor is used to initialise a new object. You pass an object to a constructor if that object is needed to perform the initialisation - for the same reason as you'd pass any other variables to a constructor.
Joined: Sep 14, 2009
Can you please broadly explain the following replied "You pass an object to a constructor if that object is needed to perform the initialisation"
Do you know what a constructor does? I think you might need to go back to a basic tutorial on them, as they'll be able to explain it much better than I can. The Java tutorials have some information here.
Once you know what a constructor is for...sometimes it needs some information to be able to do it. Providing an object might be one way of providing that information. In the Box example, you needed to supply the dimensions to initialise the Box. Providing another Box with those dimensions is just one way of doing it.
a constructor is like a group of builders. it/they make something that didn't exist before. So if you already have a house, and you want to build another house exactly like it, you can say "hey, go build me a new house that looks exactly like this existing one".
You need to tell them what the original house is to model the new one off of.
Alternatively, you could say to them "go build me a house that has 3 bedrooms, 2 baths, a walk-out basement..." and explicitly list every parameter. Neither is 'right' or 'wrong', it all depends on what you need to do.
So, if you want to create a new object that is just like one you already have, you create a constructor that takes an existing object, and use that as a model for your new object. If you don't want to do this, you certainly don't have to. It is up to the developer of the class to decide if it would be useful or not, and write hte appropriate constructors.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors