I had a doubt is there any concept like copy constructor in Java like C++. If it is there, how can it be performed in Java.Please give the reply with example.
Thanks and Regards, Prasad
prasad
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
Hi, welcome to the ranch!!
Would that look like this?
You can certainly write the code to make the constructor copy the appropriate fields from old to new, but it's not there automatically. I think the only one of these I've used in the wild is the Attributes object in the XML DOM.
oldWidget.clone() might be an alternative. The object must be written to clone properly. The code would look an awful lot like the copy constructor as it copies each field from old to new. Look at the doc for clone() on the Object class.
Either way, if you're writing the copy code it will be your choice whether to make deep copies or not.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi