File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple question" Watch "simple question" New topic

simple question

Jeffrey Pony

Joined: Jun 24, 2002
Posts: 25
How to perform shallow copy and deep copy using clone() method of object class.
Kindly give simple , precise example of each.
Thanks in advance.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Jeff P
Please note that JavaRanch has very few rules but the one we enforce with vigor is our naming standard (what would a Java programmer be without naming standards )
Please see this page:
Please adjust your display name to meet our requirements.
Thanks! And enjoy your visits to the Ranch.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Java performs shallow copies by default. Suppose you have an object, say, Thingamajig, with an instance member object called Thingy. Every Thingamajig object of course has a reference to its Thingy. You can make as many references as you like to the same Thingamajig object:

Both object references t1 and t2 point to the same object, not duplicate objects. Furthermore, both t1 and t2 also have access to the same Thingy object. This is shallow copying.
Deep copying happens when you create identical objects with the same state. If I make a deep copy of the object Thingamajig referred to by t1, I must make a separate instance of its Thingy object, as well as any other objects that the Thingy object itself may have internally, and so on recursively.
Here's a sample code;
I agree. Here's the link:
subject: simple question
It's not a secret anymore!