Results 1 to 19 of 19
  1. #1
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default Cloning problem in Java

    I'm having trouble understanding the whole process of cloning in Java. I've read everything I can find and I still don't get it. I need to build a "Defensive" class using cloning. I'm not exactly sure how to do this I get the general idea that I need to make the variables immutable so that once they are edited once the user will not be able to mess with them. Here is my code if you need any other information let me know. Thanks in advance.

    Java Code:
    /*
     * Memory.java
     */
    
    package defensive;
    
    public final class Memory implements Cloneable{
       
       //Attributes
       private String partNumber;
       private String model;
       private int capacity;
       
       // Constructors
       public Memory() {
          this("00000000", "Dummy", 0);
       }
       
       public Memory(String partNumber, String model, int capacity) {
          
          
          this.partNumber = partNumber;
          this.model = model;
          this.capacity = capacity;
       }
       
    
       //Accessors
       public String getPartNumber() {
          return partNumber;
       }
       
       public String getModel() {
          return model;
       }
       
       public int getCapacity() {
          return capacity;
       }
    
       
       
       //Mutators
       public final void setPartNumber(String partNumber) {
          this.partNumber = partNumber;
       }
       
       public final void setModel(String model) {
          this.model = model;
       }
       
       public void setCapacity(int capacity) {
          this.capacity = capacity;
       }
        @Override
       public Object clone() throws CloneNotSupportedException {
        Memory s2 = (Memory)super.clone();
        
        //s2.capacity = (Capacity)this.capacity.clone();
        return s2;
        }   
       // Behaviors
       public String toString() {
          return "Model: " + model + " (" + capacity + 
                 " GB)" + " - P/N: " + partNumber;
       }
    
    }
    that is one of four classes I can post the others if needed here is my Main...

    Java Code:
    /*
     * Main.java
     */
    
    package defensive;
    import java.lang.String;
    public class Main {
       
       public static void main(String[] args) {
          
          boolean TOGGLE = false;   //true = Demo, false = Test
          
          if (TOGGLE) {
          
             //Brittle Demo
             System.out.println("===== Begin Brittle Demo =====\n");
    
             //Build a brittle computer object
             Cpu brittleCpu = new Cpu("11111", "22222", 3.3);
             Memory brittleMemory = new Memory("44444", "55555", 6);
             MotherBoard brittleMotherBoard = 
                   new MotherBoard("77777", brittleCpu, brittleMemory);
             Computer brittleComputer = 
                   new Computer("88888", brittleMotherBoard);
             
             //Show original configuration
             System.out.println("Original Configuration\n" + brittleComputer + "\n");
    
             //Break it
             brittleMemory.setCapacity(100000000);
             //Show broken configuration
             System.out.println("Broken Configuration\n" + brittleComputer + "\n");
             
             //Create a replacement CPU and install in brittleMotherBoard
             Cpu replacementCpu = new Cpu("AAAAA", "BBBBB", 999.0);
             brittleComputer.getMotherBoard().setCpu(replacementCpu);
             //Show new configuration
             System.out.println("New Configuration\n" + brittleComputer + "\n");
             
             //Modify replacementCpu
             replacementCpu.setModel("ZZZZZ");
             //Show new broken configuration
             System.out.println("New Broken Configuration\n" + brittleComputer + "\n");
          
             //Create String representing memory model data * use to modify memory 
             String memoryModelChange = "WWWWW";
             brittleComputer.getMotherBoard().getMemory().setModel(memoryModelChange);
             
             //Show current configuration
             System.out.println("Current Configuration\n" + brittleComputer + "\n");
             
             //Modify memoryModelChange 
             memoryModelChange += "*****";
             //Show current configuration
             System.out.println("Unchanged Configuration\n" + brittleComputer + "\n");
             
          }
          
          else {
             
             //Begin Test
             System.out.println("===== Begin Bullet-Proof Testing =====\n");
    
             //Create test Cpu object and print state
             Cpu testCpu = new Cpu("GX9764CPU", "Intel 3200G", 3.2);
             System.out.println(testCpu.toString() + "\n");
    
             //Create test Memory object and print state
             Memory testMemory = new Memory("MD400SD", "Micron SDRAM", 4);
             System.out.println(testMemory.toString() + "\n");
    
             //Create test MotherBoard object and print state
             MotherBoard testMotherBoard = new MotherBoard("Intel XP3300", testCpu, 
                                                           testMemory);
             System.out.println(testMotherBoard.toString() + "\n");
    
             //Create test Computer object and print state
             Computer testComputer = new Computer("Dell PowerEdge 2100", 
                                                  testMotherBoard);
             System.out.println(testComputer.toString() + "\n");
    
             //Test #1
             System.out.println("*** Test #1 ***\n");
             //Change state of testCpu and testMemory 
             testCpu.setModel("Broken");
             testMemory.setCapacity(0);
             //Show changes have no impact on testComputer object
             System.out.println(testCpu + "\n");
             System.out.println(testMemory + "\n");
             System.out.println(testComputer + "\n");
    
             //Test #2
             System.out.println("*** Test #2 ***\n");
             //Change state of testMotherBoard
             Cpu newCpu = new Cpu("Broken", "Broken", 0.0);
             testMotherBoard.setCpu(newCpu);
             //Show changes have no impact on testComputer object
             System.out.println(testMotherBoard + "\n");
             System.out.println(testComputer + "\n");
    
             //Test #3
             System.out.println("*** Test #3 ***\n");
             //Get aggregate object of testComputer and modify it
             MotherBoard newMotherBoard = testComputer.getMotherBoard();
             //Show current state of testComputer's motherboard'
             System.out.println(newMotherBoard + "\n");
             //Change the CPU in the acquired motherboard object
             newMotherBoard.setCpu(newCpu);
             //Show current state of acquired motherboard object
             System.out.println(newMotherBoard + "\n");
             //Show changes have no impact on testComputer object
             System.out.println(testComputer + "\n");
    
             //Test #4
             System.out.println("*** Test #4 ***\n");
             //Swap out the CPU in the computer
             Cpu swapCpu = new Cpu("AZ4500CPU", "Intel 5000G", 5.0);
             MotherBoard tempMotherBoard = testComputer.getMotherBoard();
             tempMotherBoard.setCpu(swapCpu);
             testComputer.setMotherBoard(tempMotherBoard);
             //Show CPU swapped out
             System.out.println(testComputer + "\n");
             //Modify swapCpu and show no impact on testComputer
             swapCpu.setPartNumber("None");
             System.out.println(testComputer + "\n");
          
          }
          
       }
       
    }
    If any other info is needed let me know thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,577
    Rep Power
    25

    Default

    The only other info we need is what do you want from us?
    Do you have any questions? Are you getting errors?
    Is the output not what you want?
    Can you describe what you want to change?
    make the variables immutable
    Would that be like a final variable.
    Or does it mean that all the values in the object are passed in via a constructor and that there are no set.... methods that can change the variables once the object is created?

  3. #3
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    I need to keep the variables from being changed once they are initially defined. I was told to do this by cloning the data that was submitted (given in Main). In doing so if the user tries to edit the data again to "break" it. I have attached the remainding classes in which I need to clone the data in as well. Oh, when I say data I mean the objects created. They should not be able to edit the object once it is set the first time. I have attached my output thus far and the output that it needs to match.

    These are the remainding classes if needed.
    Java Code:
    /*
     * MotherBoard.java
     */
    
    package defensive;
    
    public class MotherBoard implements Cloneable{
       
       //Attributes => immutable & mutable
       private String model;
       private Cpu cpu;
       private Memory memory;
       
       // Constructors
       
       public MotherBoard() {
          this("00000000", new Cpu(), new Memory());
       }
       
       public MotherBoard(String model, Cpu cpu, 
                                 Memory memory) {
          this.model = model;
          setCpu(cpu);
          setMemory(memory);
       }
       
       // Accessors
       public Cpu getCpu() {
          return cpu;  
       }
       
       public Memory getMemory() {
          return memory;
       }
       
       // Mutators
       public void setCpu(Cpu cpu) {
          this.cpu = cpu;
       }
       
       public void setMemory(Memory memory) {
          this.memory = memory;
       }
       
       // Behaviors
       public String toString() {
          return " - Motherboard Configuration:\n    * Processor - " + 
                 cpu.toString() + "\n    * Memory - " + memory.toString();
       }
       
    }
    Java Code:
    /*
     * Cpu.java
     */
    
    package defensive;
    
    public class Cpu implements Cloneable {
       
       //Attributes
       private String partNumber;
       private String model;
       private double speed;
       
       // Constructors
       public Cpu() {
          this("00000000", "Dummy", 0.0);
       }
       
       public Cpu(String partNumber, String model, double speed) {
          
          setPartNumber(partNumber);
          setModel(model);
          setSpeed(speed);
          
       }
       
       //Accessors
       public String getPartNumber() {
          return partNumber;
       }
       
       public String getModel() {
          return model;
       }
       
       public double getSpeed() {
          return speed;
       }
       
       //Mutators
       public void setPartNumber(String partNumber) {
          this.partNumber = partNumber;
       }
       
       public void setModel(String model) {
          this.model = model;
       }
       
       public void setSpeed(double speed) {
          this.speed = speed;
       }
       
       // Behaviors
        @Override
       public String toString() {
          return "Model: " + model + " (" + speed + 
                 " GHz)" + " - P/N: " + partNumber;
       }
     
    }
    Java Code:
    /*
     * Computer.java
     */
    
    package defensive;
    
    public class Computer  {
       
       //Attributes
       private String model;
       private MotherBoard motherBoard;
       
       //Constructors
       public Computer() {
          this("00000000", new MotherBoard());
       }
       
       public Computer(String model, MotherBoard motherBoard) {
          this.model = model;
          setMotherBoard(motherBoard);
       }
       
       //Accessors
       public MotherBoard getMotherBoard() {
          return motherBoard;
       }
       
       public void setMotherBoard(MotherBoard motherBoard) {
          this.motherBoard = motherBoard;
       }
       
       //Behaviors
       public String toString() {
          return "Model: " + model + 
                 "\n" + motherBoard.toString();
       }
       
    }
    This is my current output.

    Java Code:
    ===== Begin Bullet-Proof Testing =====
    
    Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    
    Model: Micron SDRAM (4 GB) - P/N: MD400SD
    
     - Motherboard Configuration:
        * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
        * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    
    Model: Dell PowerEdge 2100
     - Motherboard Configuration:
        * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
        * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    
    *** Test #1 ***
    
    Model: Broken (3.2 GHz) - P/N: GX9764CPU
    
    Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
    Model: Dell PowerEdge 2100
     - Motherboard Configuration:
        * Processor - Model: Broken (3.2 GHz) - P/N: GX9764CPU
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
    *** Test #2 ***
    
     - Motherboard Configuration:
        * Processor - Model: Broken (0.0 GHz) - P/N: Broken
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
    Model: Dell PowerEdge 2100
     - Motherboard Configuration:
        * Processor - Model: Broken (0.0 GHz) - P/N: Broken
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
    *** Test #3 ***
    
     - Motherboard Configuration:
        * Processor - Model: Broken (0.0 GHz) - P/N: Broken
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
     - Motherboard Configuration:
        * Processor - Model: Broken (0.0 GHz) - P/N: Broken
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
    Model: Dell PowerEdge 2100
     - Motherboard Configuration:
        * Processor - Model: Broken (0.0 GHz) - P/N: Broken
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
    *** Test #4 ***
    
    Model: Dell PowerEdge 2100
     - Motherboard Configuration:
        * Processor - Model: Intel 5000G (5.0 GHz) - P/N: AZ4500CPU
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    
    Model: Dell PowerEdge 2100
     - Motherboard Configuration:
        * Processor - Model: Intel 5000G (5.0 GHz) - P/N: None
        * Memory - Model: Micron SDRAM (0 GB) - P/N: MD400SD
    And this is the output that I need to match.

    Java Code:
    ===== Begin Bullet-Proof Testing =====
    Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    Model: Micron SDRAM (4 GB) - P/N: MD400SD
    - Motherboard Configuration:
    * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Computer Configuration ***
    - Model: Dell PowerEdge 2100
    - Motherboard Configuration:
    * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Test #1 ***
    Model: Broken (3.2 GHz) - P/N: GX9764CPU
    Model: Micron SDRAM (0 GB) - P/N: MD400SD
    *** Computer Configuration ***
    - Model: Dell PowerEdge 2100
    - Motherboard Configuration:
    * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Test #2 ***
    - Motherboard Configuration:
    * Processor - Model: Broken (0.0 GHz) - P/N: Broken
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Computer Configuration ***
    - Model: Dell PowerEdge 2100
    - Motherboard Configuration:
    * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Test #3 ***
    - Motherboard Configuration:
    * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    - Motherboard Configuration:
    * Processor - Model: Broken (0.0 GHz) - P/N: Broken
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Computer Configuration ***
    - Model: Dell PowerEdge 2100
    - Motherboard Configuration:
    * Processor - Model: Intel 3200G (3.2 GHz) - P/N: GX9764CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Test #4 ***
    *** Computer Configuration ***
    - Model: Dell PowerEdge 2100
    - Motherboard Configuration:
    * Processor - Model: Intel 5000G (5.0 GHz) - P/N: AZ4500CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    *** Computer Configuration ***
    - Model: Dell PowerEdge 2100
    - Motherboard Configuration:
    * Processor - Model: Intel 5000G (5.0 GHz) - P/N: AZ4500CPU
    * Memory - Model: Micron SDRAM (4 GB) - P/N: MD400SD
    Thanks for the help.

  4. #4
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    I am guessing I am going to have to Deep clone the objects of each of the provided classes how would I do that?

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Cloning Objects

    Making Deep Copies of Objects

    How to avoid traps and correctly override methods from java.lang.Object

    Attack of the clones
    Java Code:
    import java.awt.Point;
    
    public class CloneTest {
        public static void main(String[] args)
                    throws CloneNotSupportedException {
            CloneMe cm1 = new CloneMe(10, 25, 12);
            CloneMe cm2 = (CloneMe)cm1.clone();
            System.out.printf("cm1 = %s%ncm2 = %s%n",
                               cm1, cm2);
            cm1.p.x = 35;
            System.out.printf("cm1 = %s%ncm2 = %s%n",
                               cm1, cm2);
        }
    }
    
    class CloneMe implements Cloneable {
        Point p = new Point();
        int z = 12;
    
        public CloneMe(int x, int y, int z) {
           p.setLocation(x, y);
           this.z = z;
        }
    
        public Object clone() throws CloneNotSupportedException {
            CloneMe newObj = (CloneMe)super.clone();
            // Comment-out this next line for shallow copy.
            newObj.p = (Point)this.p.clone();
            return newObj;
        }
    
        public String toString() {
            return getClass().getName() + "[p=" +
                   p.getClass().getSimpleName() +
                   "[" + p.x + ", " + p.y + "]; z=" + z + "]";
        }
    }

  6. #6
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Thanks for the example but now I am getting this error clone() has protected access in java.lang.Object. I've tried everything I can think of to fix it but no good. here is my code if you could tell me what Im doing wrong.

    This is where it says the error is. newMemory.model = (String)this.model.clone();

    Java Code:
    /*
     * Memory.java
     */
    
    package defensive;
    
    
    public class Memory implements Cloneable{
       
       //Attributes
       private String partNumber;
       private String model;
       private int capacity;
       
       // Constructors
       public Memory() {
          this("00000000", "Dummy", 0);
       }
       
       public Memory(String partNumber, String model, int capacity) {
          
          this.partNumber = partNumber;
          this.model = model;
          this.capacity = capacity;
       }
       
    
       //Accessors
       public String getPartNumber() {
          return partNumber;
       }
       
       public String getModel() {
          return model;
       }
       
       public int getCapacity() {
          return capacity;
       }
    
       
       
       //Mutators
       public void setPartNumber(String partNumber) {
          this.partNumber = partNumber;
       }
       
       public final void setModel(String model) {
          this.model = model;
       }
       
       public void setCapacity(int capacity) {
          this.capacity = capacity;
       }
        @Override
       public Object clone() throws CloneNotSupportedException{
                    
            Memory newMemory = (Memory)super.clone();
            newMemory.model = (String)this.model.clone();
            return newMemory;
    
        }
       // Behaviors
       public String toString() {
          return "Model: " + model + " (" + capacity + 
                 " GB)" + " - P/N: " + partNumber;
       }
    
    }

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Have you tried to make your clone() method protected instead of public?

  8. #8
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    still gives same error

  9. #9
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    Have you tried to make your clone() method protected instead of public?
    I would say maybe the design, Abstract class may suit your needs better?

    cheers

    Ian G.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,577
    Rep Power
    25

    Default

    When you get an error, please post the full text of the message without editing or changing it.

  11. #11
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Here is the full error

    Compiling 2 source files to C:\Documents and Settings\Jake\My Documents\NetBeansProjects\Defensive\build\classes
    C:\Documents and Settings\Jake\My Documents\NetBeansProjects\Defensive\src\defensive \Memory.java:19: clone() has protected access in java.lang.Object
    clonedMemory.partNumber = (String)partNumber.clone();
    1 error
    BUILD FAILED (total time: 0 seconds)

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,577
    Rep Power
    25

    Default

    Why would you need to clone a String object. Since its contents can't be changed, copying a reference to it would be enough.

  13. #13
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Right okay. Well what about when I try to clone capacity? I get this error.

    C:\Documents and Settings\Jake\My Documents\NetBeansProjects\Defensive\src\defensive \Memory.java:34: int cannot be dereferenced
    clonedMemory.capacity = (Integer)this.capacity.clone();
    1 error
    BUILD FAILED (total time: 0 seconds)

  14. #14
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Shouldn't when I cloned Memory all of the variables be cloned as well?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,577
    Rep Power
    25

    Default

    Integer is like String, its contents can't be changed, so a reference to it gives you its value without worrying about some one changing it.
    Can you show the definitions of the variables referenced in the error messages?

  16. #16
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Not sure what you mean by definition. I have only been messing around with one class and here is what I have.

    Java Code:
    package defensive;
    
    
    public class Memory implements Cloneable{
       
       //Attributes
       private String partNumber;
       private String model;
       private int capacity;
       
       //Creation of Deep Clone method.
        @Override
        public Object clone() {
            String clonedModel;
            String clonedPartNumber;
            int clonedCapacity;
                try {
                    super.clone();
                    //model = clonedModel;
                    //capacity = clonedCapacity;
                    //partNumber = clonedPartNumber;
                    //Memory clonedMemory = (Memory)super.clone();
                    //clonedMemory.capacity = (Integer)capacity.clone();
                    //clonedMemory.capacity = (Integer)this.capacity.clone();
                    return super.clone();//clonedModel, clonedCapacity, clonedPartNumber;
                }
                catch (CloneNotSupportedException e) {
                    throw new InternalError(e.toString());
                }
            }
       
       // Constructors
       public Memory() throws CloneNotSupportedException {
          this("00000000", "Dummy", 0);
       }
       
       public Memory(String partNumber, String model, int capacity) throws CloneNotSupportedException {
          Memory clonedMemory = (Memory)super.clone();
          this.partNumber = partNumber;
          this.model = model;
          this.capacity = capacity;
          
       }
       
    
       //Accessors
       public String getPartNumber() {
          return partNumber;
       }
       
       public String getModel() {
          return model;
       }
       
       public int getCapacity() {
          return capacity;
       }
    
       
       
       //Mutators
       public void setPartNumber(String partNumber) {
          this.partNumber = partNumber;
       }
       
       public final void setModel(String model) {
          this.model = model;
       }
       
       public void setCapacity(int capacity) {
          this.capacity = capacity;
       }
        //@Override
       //protected Object clone() throws CloneNotSupportedException{
                    
            //Memory clonedMemory = (Memory)super.clone();
            //newMemory.model = (String)this.model.clone();
            //return clonedMemory;
    
        //}
       // Behaviors
       public String toString() {
          return "Model: " + model + " (" + capacity + 
                 " GB)" + " - P/N: " + partNumber;
       }
    
    }
    No matter what I do main still manages to edit the object. How would I pass the cloned object back to Main assuming I have implemented cloning correctly?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,577
    Rep Power
    25

    Default

    Not sure what you mean by definition
    int capacity; // define the variable capacity

    main still manages to edit the object
    Which object? If an object is editable, it will always be editable.
    Do you have any println() output to show what you mean?

    The question is: Can you have one copy of an object in class A that you clone and pass the clone to class B and be able to prevent class B from changing anything in the original class object saved in class A. If you deep copy, there won't be any way for class B to change the original object that is saved in class A.

  18. #18
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Okay well am I implementing Deep Copy correctly?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,577
    Rep Power
    25

    Default

    Write a test program to see.
    Create an object.
    Display a mutable field.
    Clone a copy of that object.
    Display the same mutable field.
    Change the field in the clone.
    Display the field for the original object.
    That will show you if the clone is a copy or if it shares a mutable object.

Similar Threads

  1. Java problem
    By grend in forum New To Java
    Replies: 5
    Last Post: 08-18-2008, 11:44 PM
  2. folder cloning
    By jad in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2008, 12:28 AM
  3. Cloning objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 09:33 AM
  4. Cloning in Java
    By gchalpatirao in forum Advanced Java
    Replies: 0
    Last Post: 08-14-2007, 03:29 PM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 10:10 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
  •