Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

how to clone an Object

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have got a problem in cloning an object. I am passing a vector, containing referenes to custom Objects, as an argument to a method. I need to manipulate with the custom Objects in the called method without changing the data in original copy. How can I achieve it by cloning my custom Object.
For example, I was trying as follows..
class TestClone {
public static void main(String args[]){
Vector v = new Vector();
v.add(new Argument(1,2));
v.add(new Argument(3,4));
for(int i=0; i<v.size(); i++){>
MyObject a = (MyObject )v.get(i);
System.out.println("i=" +a.getI() + " j="+a.getJ());
}
TestClone t = new TestClone();
t.changeObject(v);
System.out.println("after invoking method");
for(int i=0; i<v.size(); i++){>
MyObject a = (MyObject)v.get(i);
System.out.println("i=" +a.getI() + " j="+a.getJ());
}
}
void changeObject(Vector v){
Vector ve = v;
MyObject a = (MyObject)(ve.get(1));
a.setI(5);
a.setJ(6);
}
}

class MyObject implements Cloneable{
int i,j;
MyObject (int i, int j){
this.i = i;
this.j = j;
}
int getI(){
return i;
}
int getJ(){
return j;
}
void setI(int i){
this.i = i;
}
void setJ(int j){
this.j = j;
}
}

OUTPUT(Acutal):
i=1 j=2
i=3 j=4
after invoking method
i=1 j=2
i=5 j=6

OUTPUT (Expected):
i=1 j=2
i=3 j=4
after invoking method
i=1 j=2
i=3 j=4

I really appreciate if anybody can help me to get the expected result.
thanks
venkat
[This message has been edited by venkat paruchuri (edited November 14, 2001).]
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure of what you want but here is a solution to get the expected result (changes appear in bold).
class TestClone {
void changeObject(Vector v){
Vector ve = v;
MyObject a = (MyObject)(ve.get(1));
MyObject b= a.clone();
b.setI(5);
b.setJ(6);
}
}

class MyObject implements Cloneable{
int i,j;
MyObject (int i, int j){
this.i = i;
this.j = j;
}
public Object clone() {
MyObject tbr=new MyObject(i,j);
return tbr;
}

}

W.

[This message has been edited by Wilfried LAURENT (edited November 15, 2001).]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or there is this nasty piece of code that provides a cheat for a deep clone...

Dave.
(this code must be used for good only, never evil )
 
venkat paruchuri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Wilfried ,
Thank you very much for having provided solution to my problem. If you don't mind I have a question in your solution.
I could get the cloning object without implementing MyObject. Instead of overriding the clone() method of Object Class, I could do it as follows..


MyObject getClone(){
return new MyObject(i,j);
}


As we know, by default every object is subclassed from Object. My doubt here is, what could be the advantage of overriding the clone() method of Object.
I really appreciate if you can find time to reply me. Looking forward for your reply.
thanks
venkat

 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While implementing the Cloneable interface, your class is saying to the world "Eh, Look! I can be cloned!". If you provide your own method doing the same thing with a different name, the client may not be aware that you can get a copy of your object.
You will lose in genericity.
In your example, this might not be so pertinent, as long as you know the objects you are handling and you know that a getClone() method exists.
But imagine a generic Hashtable allowing a deep clone of its instance (which is not the case -only shallow clone).
What you will do is enumerate all the elements in your table, check if they are Cloneable and then apply clone() on the Object. If the objects stored in the hashtable does not all provide a similar method for cloning then you can not be generic. clone() is this method because you know every object potentially has this method inherited from the Object class. This has to do with polymorphism and if you are not familiar with, it can not be obvious.
W.


[This message has been edited by Wilfried LAURENT (edited November 16, 2001).]
 
venkat paruchuri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wilfried ,
Thank you for your nice explanation about cloning. Yea..I agree with you that, overriding the clone() method is good practice of following OOPS concept.
I got one more doubt in overriding the clone() method of the example which you discussed above. In the you have returned as,
new MyObject(i,j).
you are trying to return the object with all the values with the help of construtor. suppose there are 10 variables in MyObject and the class has only two argument constructor and several methods to manipulate the other variables. In such a situation, how can I clone MyObject with just a two argument constructor. please suggest me if you have any way for that. Ofcourse, I will aslo give some thought and will come up with a soultion. Looking forward for your reply
thanks
venkat
 
venkat paruchuri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
thak you very much for your reply. I look forward for your help in future also.
thanks
venkat
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the variables are primitive (int, char etc) then it is straightforward.

Now if the variables refer to Objects it gets tricky, but if all the classes implemented Clone() with code like this:
tbr.var1 = this.var1.Clone();
then it should lead to recursive calls along the Object graph and you get a full and deep Object copy.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic