?
Solved

clone still pointing to original object

Posted on 2005-03-28
3
Medium Priority
?
261 Views
Last Modified: 2010-03-31
I cloned an object of my own class, but it still points to the same object.  I mean, when I make changes to the cloned object the original is changed as well.  How do I do this properly.  I implemented Cloneable and and overid clone().  So whats wrong.

public Object clone() {
        try {
            return super.clone();
        }
        catch (CloneNotSupportedException e) {
            throw new InternalError(e.toString());
        }
    }

.................


players = (Player[])playersInfo.clone();

.............
0
Comment
Question by:turtletimer
[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
  • 2
3 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13649169
players = (Player[])playersInfo.clone(); -> this does not clone Player object but rather clone the array of players.
the cloned array will point to the same objects.
0
 
LVL 15

Accepted Solution

by:
aozarov earned 2000 total points
ID: 13649174
If you want to clone the whole array then you should do
Player[] newPlayers = new Player[playersInfo.length];
for (int i = 0; i < playersInfo.length; i++)
newPlayers[i] = (Player) playersInfo[i].clone();
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13649387
The default/conventional behavior of clone() is to do a shallow copy. You can either override clone() to do a deep copy or provide a separate method to do a deep clone.

The simplest approach to deep cloning is to use Java serialization, where you serialize and deserialize the object and return the deserialized version. This will be a deep copy/clone, assuming everything in the tree is serializable. If everything is not serializable, you'll have to implement the deep cloning behavior yourself.

Assuming everything is serializable, the following should create a complete deep copy of the current class instance:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Object deepCopy = ois.readObject();

Refer to

http://www.churchillobjects.com/c/11091d.html

-CE
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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