Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-22
17
Medium Priority
?
323 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 300 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

916 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