aspose file tools*
The moose likes Beginning Java and the fly likes Constructor basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor basics" Watch "Constructor basics" New topic
Author

Constructor basics

ppavya india
Greenhorn

Joined: Sep 27, 2004
Posts: 19
Hi all,
I need some help in the program given below.This program contents some constructor basics, but I haven't understood two method and some statements, could you please explain me what are they.I have marked those methods and statements with comments as //didn't get******************
Any help would be appreciated.
Thanx in advance
class box
{
int l,b,h;
box(int i)
{
l=b=h=i;
}
box(int l, int b, int h)
{
this.l=l;
this.b=b;
this.h=h;
}
box (box b)//didn't get****************************************
{
this.l=b.l;
this.b=b.b;
this.h=b.h;
}

public void volume()
{
System.out.println(l*b*h);
}
public static box clone(box b)//didn't get********************
{
return new box (b.l,b.b,b.h);//didn't get***********************
}
}
class test
{
public static void main(String ab[])
{
box b2,b3,b4,b5;
b2=new box(10);
b3=new box(10,20,30);
b2.volume();
b3.volume();
System.out.println("After creating a clone");
//b4=box.clone (b2); //didn't get************************
//or
b4=new box(b2);
b4.volume();
//b5=box.clone(b3);//didn't get***************************
//or
b5=new box(b3);
b5.volume();
}
}
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
box (box b) this is called copy constructor. It take as parameter itself type and do some action on that instance, which produces the copy of that object. I was using this in C++ because of pointers. C++ has implicit copy constructor but this one did only the shallow copy, it means only copy of pointers no copy of data.
Here it is the same situation, you do a clone of some object. You want it will have the same property as the old one.



SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor basics