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 4 of 4

Thread: Copying Objects

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Copying Objects

    How do you copy objects in Java? Every copy I currently make points to the same location in memory. Really frustrating as I transition from C++.

    The object I want to make a copy of also is holding an object from the Calendar Class inside of it.


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

    Default Re: Copying Objects

    You must create a deep copy of the object, and there are many ways to do so (constructors, setters, Object.clone). For more information on cloneable, shallow copies, and deep copies see clone (Java method) - Wikipedia, the free encyclopedia

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Copying Objects

    I guess you're copying the object reference rather than the object itself. You'll find that object references in Java are a bit like pointers in C++ (with some restrictions). You never really see a 'raw' object in Java, they're always handled by reference variables which refer (point) to the actual object. When you declare a Java object reference variable, it is null until you initialize it to refer (point) to an object of that type.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copying Objects

    If you have copied the object to new object using assignment operator then it surely is an object reference that will point to earlier object. That could be the reason you see same copies through different object reference. In case if you are looking for multiple copies you should clone the original object.

Similar Threads

  1. New to Objects...
    By Java Neil in forum What's Wrong With My Code?
    Replies: 17
    Last Post: March 24th, 2011, 07:00 AM
  2. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 12:01 PM
  3. need help copying (what is in address bar)
    By 3ammary in forum Java Networking
    Replies: 0
    Last Post: January 8th, 2011, 09:32 AM
  4. Help with background and objects
    By Afromiffo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2010, 01:19 AM
  5. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM