Solved

Deep Copy in Java

Posted on 2010-09-08
5
636 Views
Last Modified: 2012-06-21
Since I've been programming a lot with C and C++, I am a bit confused as to how I'm going to implement something in Java since it manages memory it's own way. Take the following steps for example:

I create a hierarchy of classes, and create an instance of an this object (polymorphism stuff) with specific data values and store it.

Simple enough right?

I will call this my TEMPLATE OBJECT

Now, I want to copy my template object to another object called HELLO, and it needs to be a DEEP COPY because I want to change HELLO in every way, and have the TEMPLATE object unaltered.

How do I do this in Java??

I'm confused because you can't specifically code in a copy constructor or anything like that in Java.

Can I do something like. public example_object temp = new example_object(TEMPLATE)   --??

Thanks Experts!!!

I REALLY NEED HELP ON THIS ONE. It's for homework and I gotta get crackin, I'll be sitting by this post. Lets see what ya'll think.

-Jeff
0
Comment
Question by:jeffiepoo
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 33632313
you need to implement your own copy constructor

another way is to use serialization
http://javatechniques.com/blog/faster-deep-copies-of-java-objects/
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33632928
what I have done in the past was to create a deepCopy in each class

my setup was like this

object.deepCopy(oldObject)

this.f1 = oldObject.f1
this.f1 = oldObject.f2
etc

So the first deepCopy you call, it copies the primitive types like ints, strings etc


Now this class had another class (like yours), I would instantiate a new object and call that deepCopy
eg

this.obj1 = new OtherClass()
this.obj1.deepCopy(oldObject.getObj1())

and so on



0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 150 total points
ID: 33634333
Take a look at method clone(). But read the specification carefully because in order to achieve "deep copy" you have to follow some requirements. Otherwise it is "shallow copy".
0
 
LVL 7

Accepted Solution

by:
sailingbye earned 350 total points
ID: 33637748
A simple example of what Valeri is suggesting is attached.

Note, that if the data is not a simple type and a deep copy is required, it may be necessary to clone the data too.
public class MyClass implements Cloneable {
 
  int data;

  public MyClass (int data) {
    this.data = data;
  }
  
  public Object clone () {
    return new MyClass(data);
  }
}

Open in new window

0
 
LVL 16

Expert Comment

by:Valeri
ID: 33637882
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now