Results 1 to 5 of 5

Thread: Cloning problem

  1. #1
    guilty is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default Cloning problem

    Here's my class:
    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;
        }
    }
    and here's my "main":
    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());
        }
    }
    Output:
    Java Code:
    1st object: 3, 9, 27
    2nd object: 3, 9, 27
    Question:
    wtf?

    Wished result:
    Java Code:
    1st object: 2, 4, 8
    2nd object: 3, 9, 27
    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 :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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;
       }

  3. #3
    guilty is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    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.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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

  1. Cloning issue
    By Paul Richards in forum Advanced Java
    Replies: 2
    Last Post: 08-26-2009, 11:29 PM
  2. Cloning problem in Java
    By jralexander137 in forum New To Java
    Replies: 18
    Last Post: 10-30-2008, 10:52 PM
  3. Help with Deep Cloning
    By jralexander137 in forum New To Java
    Replies: 6
    Last Post: 10-30-2008, 05:34 PM
  4. folder cloning
    By jad in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2008, 12:28 AM
  5. Cloning objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 09:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •