Results 1 to 5 of 5
Thread: Cloning problem
- 11-13-2010, 10:29 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Cloning problem
Here's my class:
and here's my "main":Java Code://File: TestClass.java public class TestClass implements Cloneable { private int[] array = {0, 0, 0}; public TestClass(int x, int y, int z) { this.set(x, y, z); } public void set(int x, int y, int z) { this.array[0] = x; this.array[1] = y; this.array[2] = z; } public int getX() { return array[0]; } public int getY() { return array[1]; } public int getZ() { return array[2]; } @Override public Object clone() throws CloneNotSupportedException { TestClass retVal = (TestClass) super.clone(); retVal.set(this.array[0], this.array[1], this.array[2]); return retVal; } }
Output:Java Code://File Main.java public class Main { public static void main(String[] args) { TestClass testObject1 = new TestClass(2, 4, 8); TestClass testObject2 = null; try { testObject2 = (TestClass) testObject1.clone(); } catch (CloneNotSupportedException e) {} testObject2.set(3, 9, 27); System.out.println("1st object: " + testObject1.getX() + ", " + testObject1.getY() + ", " + testObject1.getZ()); System.out.println("2nd object: " + testObject2.getX() + ", " + testObject2.getY() + ", " + testObject2.getZ()); } }
Question:Java Code:1st object: 3, 9, 27 2nd object: 3, 9, 27
wtf?
Wished result:
I have no idea what's wrong. Well I suspect that array[n] is a reference, not an actual int, but how do I make it right? Hope I made my question clear :)Java Code:1st object: 2, 4, 8 2nd object: 3, 9, 27
-
You're only doing a shallow copy of the class and instead need to do a deep copy. I'm no expert in this, so any corrections are appreciated, but I think it's something like so:
Java Code:@Override public Object clone() throws CloneNotSupportedException { TestClass retVal = (TestClass) super.clone(); // make a clone of the array int[] cloneArray = (int[])array.clone(); // set the cloned object's array to the cloned array. retVal.array = cloneArray; return retVal; }
- 11-14-2010, 09:07 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
I don't know why, but I thought, that I couldn't access "retVal" array, because that array is public. Turns out I can! :) Thanks.
- 11-14-2010, 09:59 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You mean private, not public. And think about it this way, a private instance can only be accessed from the current class:
Java Code:public class MyClass { private int bla; //methods, constructors... public void doSomething() { MyClass m = new MyClass(); m.bla = 5; //well, you're still in MyClass, why shouldn't you be able to acces the field bla? } }Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-14-2010, 06:06 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Yup, you can access any variable of any instance of the class you're in. Same applies for protected, so basically, if you can access this.someField, you can access otherInstance.someField.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Cloning issue
By Paul Richards in forum Advanced JavaReplies: 2Last Post: 08-26-2009, 11:29 PM -
Cloning problem in Java
By jralexander137 in forum New To JavaReplies: 18Last Post: 10-30-2008, 10:52 PM -
Help with Deep Cloning
By jralexander137 in forum New To JavaReplies: 6Last Post: 10-30-2008, 05:34 PM -
folder cloning
By jad in forum Advanced JavaReplies: 1Last Post: 07-01-2008, 12:28 AM -
Cloning objects
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks