Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: how to implement deep cloning without serialization?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to implement deep cloning without serialization?

    i stumbled upon this problem....pls suggest some way

    --- Update ---

    i tried the following code ...but it is not working:
    class DeepCopy implements Cloneable{
    String par;
    DeepCopy o1;
    public static void main(String a[])
    {
    DeepCopy dummy=new DeepCopy();
    dummy.go();
    }
    void go(){
    o1=new DeepCopy();
    o1.par="33";
    DeepCopy o2=(DeepCopy)o1.clone();
    System.out.println(o2.par);
    }
    public Object clone(){
    try{
    DeepCopy o2=(DeepCopy)super.clone();
    o2.par=(String)o1.par.clone();
    return o2;
    }
    catch(CloneNotSupportedException e)
    {
    return null;
    }
    }
    }
    Last edited by curmudgeon; December 6th, 2012 at 11:08 AM. Reason: code tags added


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to implement deep cloning without serialization?

    Define 'it is not working'...does it compile? Are there exceptions? Does it misbehave? Post all errors in their entirety

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to implement deep cloning without serialization?

    Sorry for not defining clearly earlier......
    in the line
    o2.par=(String)o1.par.clone();
    it is giving compiler error saying clone method has protected access in Object
    But i have already overriden the clone method in the code above

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to implement deep cloning without serialization?

    String does not override the clone method, thus it relies on its parent class (Object) clone method, which is protected. Just create a new String from the old

  5. The Following User Says Thank You to copeg For This Useful Post:

    shreerangpande (December 6th, 2012)

  6. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: how to implement deep cloning without serialization?

    I've edited your original post and have added [code] [/code] tags to allow the posted code to show its formatting, but see that even with the tags, the code you've posted was non-formatted. Please consider correcting that.

  7. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to implement deep cloning without serialization?

    [B]Thanks for ur reply:
    i tried out what u suggested:
    import java.util.*;
    class DeepCopy1 implements Cloneable{
    String par;
    DeepCopy1 o1,o2;
    public static void main(String a[])
    {
           DeepCopy1 dummy=new DeepCopy1();
           dummy.go();
    }
    void go(){
              o1=new DeepCopy1();
              o1.par="33";
              o2=(DeepCopy1)o1.clone();
              System.out.println(o2.par);
              o1.par="44";
              System.out.println(o2.par);
    }
    public Object clone(){
     try{
            DeepCopy1 o2=(DeepCopy1)super.clone();
             o2.par=o1.par;
            return o2;
    }
     catch(CloneNotSupportedException e)
    {
           return null;
    }
    }
    }
    Error i m getting is: null pointer exception at line:

    o2.par=o1.par;

  8. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: how to implement deep cloning without serialization?

    o1 is null. Where is it initialized? Why are you even using o1 here?

  9. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to implement deep cloning without serialization?

    Sir,
    o1 is initialized in the go method above....also i have made o1 an instance variable so that it can be accesed from any other method.
    i want to make a deep clone of o1 and name it o2....so i m using o1.

  10. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: how to implement deep cloning without serialization?

    And where do you call go() with relation to your cloning attempt? Is it called before the attempt? But more importantly why do you even need o1 when you are trying to copy when you can just use the field? You are ultimately trying to make a deep copy of DeepCopy1, not of o1.

  11. The Following User Says Thank You to curmudgeon For This Useful Post:

    shreerangpande (December 7th, 2012)

  12. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to implement deep cloning without serialization?

    Thanks for ur reply....with ur help, finally my code is working

Similar Threads

  1. JDBC vs Serialization
    By hafunui in forum Java Theory & Questions
    Replies: 2
    Last Post: May 1st, 2012, 05:02 PM
  2. Array in constructor and deep copy.
    By Ejii in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 08:34 PM
  3. DefaultTableModel Serialization
    By newbie in forum Java Theory & Questions
    Replies: 8
    Last Post: August 15th, 2011, 07:46 PM
  4. Deep copy of ArrayList?
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2011, 01:30 PM
  5. serialization problem
    By Park in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2009, 04:44 PM