aspose file tools*
The moose likes Beginning Java and the fly likes What does 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 » Beginning Java
Bookmark "What does "pass object to constuctor" mean? " Watch "What does "pass object to constuctor" mean? " New topic
Author

What does "pass object to constuctor" mean?

Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 43
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Sep 14, 2009
Posts: 43
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Sep 14, 2009
Posts: 43
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Oct 02, 2003
Posts: 10911
    
  12

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
. . . 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does "pass object to constuctor" mean?
 
Similar Threads
Inheritance, and the mis-use of the 'static' keyword
Return Statements
End of file while parsing
toString method
can we invoke an overloaded constructor by object of same class?