Solved

Deep Copy in Java

Posted on 2010-09-08
5
635 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

16 Experts available now in Live!

Get 1:1 Help Now