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
Solved

Deep Copy in Java

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
Oracle SQL syntax check  without executing 6 74
ejb wildfly example 2 16
ejb entity bean example 2 7
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

791 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