Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does "pass object to constuctor" mean?

 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a new bee to java. I came across the below code in which we pass class type as an argument to a constructor.



Please help me to understand what is the purpose of pass object to constructor? What happens when we do it? In which scenarios it is useful?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please broadly explain the following replied "You pass an object to a constructor if that object is needed to perform the initialisation"
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiny point:

You don't actually pass the object to the constructor, or to anything else. You pass a number, which allows the JVM to find the memory address of that object.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and that number is hidden behind the variable you pass to the constructor. You are passing the number hidden behind myBox1 and in the constructor you hide that same number behind ob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic