Solved

Cloneable class ... is cloning an object the same as creating an object with new?

Posted on 2006-06-22
17
316 Views
Last Modified: 2012-06-27
I'm not really underanding how cloneable works, I expected it to work like a copy constructor in C++ but I'm getting odd results. Im wondering if this is due to my misunderstanding of cloneable.

if I have sometthing like:

class G implements Cloneable{

private Vector a;
private Vector b;

//methods to modify vectors
...

public Object clone(){
            
      try{
            G cloned = (G)super.clone();
      
            cloned.a= (Vector)a.clone();
            cloned.b= (Vector)b.clone();
            
            return cloned;
      }catch (CloneNotSupportedException e) { return null; }
}
      
//... then I use it in another class where I generate clones and add them to another list

class UseG{

Vector lotsOfGs;


//methods...
public void somemethod(G gg){

G clonedGG = (G)gg.clone();

System.out.print(clonedGG ); //1

lotsOfGs.addElement(clonedGG); //2

}


The weird results are that the set of G's printed by 1 are totally different than the set of the those in 2
Any ideas?
0
Comment
Question by:polkadot
  • 6
  • 6
  • 5
17 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 16965281
I am not sure of the C++ functionality but here it you responsibility to make the clone to work as you wanted. Vector has it own implementation of clone where in the cloned vector will have the same elements of source.

Note: No default functionality provided for clone.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16965288
below is the method description from Sun for the clone method in Object class,

"Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:
 x.clone() != xwill be true, and that the expression:
 x.clone().getClass() == x.getClass()will be true, but these are not absolute requirements. While it is typically the case that:
 x.clone().equals(x)will be true, this is not an absolute requirement.
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time."
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 100 total points
ID: 16965313
>>Note: No default functionality provided for clone

Sorry this is not correct exactly, actually if you implement cloneable interface, the default functionality is provided through reflection.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 26

Expert Comment

by:ksivananth
ID: 16965320
0
 

Author Comment

by:polkadot
ID: 16966009
So, in my example, am I doing enough to implement a deep and accurate copy/clone of the G class?
0
 
LVL 92

Expert Comment

by:objects
ID: 16966428
Deep as in it copies the vector in your objects, but not a deep copuy of the Vectors. The Vector copies will contain a reference to a clone of the internal data array, not a reference to the original internal data array of each Vector.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16971578
ya, you have to extend Vector and override the clone method to do a deep copy of the elements of the internal array.
0
 

Author Comment

by:polkadot
ID: 16973966
Im not sure how to extend Vector and override the clone method? Can you give me an example?
0
 
LVL 92

Expert Comment

by:objects
ID: 16974023
why do u need to . Exactly what is it u are trying to achieve?
0
 

Author Comment

by:polkadot
ID: 16974045
I need a deep copy of the Vectors and Lists in my cloned objects.

See my example above ... with out this, //1 and //2 produce different sets.
0
 
LVL 92

Expert Comment

by:objects
ID: 16974056
what do you mean they produce different sets
//1 is printing out the clone, and //2 is adding it to something. What exectly is different?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16974058
0
 

Author Comment

by:polkadot
ID: 16975072
what do you mean they produce different sets
//1 is printing out the clone, and //2 is adding it to something. What exectly is different?

Programmatically, I don't know what is different, hence my question.

The clonedGGs aren't being stored in the vector the same way they are printing ...

The values in the list are different than what is printed.

I'm using many different types of vectors in my code, is it more efficient to extend the vector class and override cloneable so that the vector makes a deep copy?
0
 
LVL 92

Expert Comment

by:objects
ID: 16975115
wi'm confused, what are u basing this on? The code u have posted only prints one instance, what r u comparing i to?
0
 

Author Comment

by:polkadot
ID: 16975524
... oh ... after the set "lotsOfGs" has all its elements added ... I print it out and none of the elements match what the "system out print" generated.
0
 
LVL 92

Expert Comment

by:objects
ID: 16977026
don';t think thats anything to do with the cloning.
can u post an example that demonstates problem?
0
 

Author Comment

by:polkadot
ID: 16977424
actually, the cloneable method was the entire problem, I have it fixed now by making the class serializable and following the web site suggested:
http://www.javaworld.com/javaworld/javatips/jw-javatip76.html

Thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
JDeveloper 12c for 32 bit 4 84
maven project error 5 54
use lov values 2 49
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

813 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

12 Experts available now in Live!

Get 1:1 Help Now