Solved

Deep Copy in Java

Posted on 2010-09-08
5
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

710 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