Meaningless Drivel is fun!
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor basics" Watch "Constructor basics" New topic

Constructor basics

ppavya india

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)
box(int l, int b, int h)
box (box b)//didn't get****************************************

public void volume()
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);
System.out.println("After creating a clone");
//b4=box.clone (b2); //didn't get************************
b4=new box(b2);
//b5=box.clone(b3);//didn't get***************************
b5=new box(b3);
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:
subject: Constructor basics
It's not a secret anymore!