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

Thread: clone method in java

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default clone method in java

    Hi,

    I heard that clone() method in java creates a copy of an object without calling the constructor. how does it create an another object without calling the constructor? i tried looking into source code of java.lang.Object clone() but it is native method. can anyone explain this?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: clone method in java

    I think you may have misunderstood. Provide a reference for what you "heard", if possible.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: clone method in java

    Quote Originally Posted by GregBrannon View Post
    I think you may have misunderstood. Provide a reference for what you "heard", if possible.
    Hi,

    Thanks for the reply. I shall give you an example.

    public class CloningEx implements Cloneable{
    int b;
    CloningEx(){
    System.out.println("Default constructor");
    }

    public static void main(String ar[]) throws CloneNotSupportedException{
    CloningEx ce = new CloningEx();
    ce.b = 1000;

    CloningEx ce1 = (CloningEx)ce.clone();
    System.out.println(ce1.b);
    }

    }

    in the above example, when i call clone method, the constructor is not getting called.
    the o/p is:

    Default constructor -- (this is for CloningEx ce = new CloningEx()
    1000

    How does a clone method work?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: clone method in java

    The "default" clone method of Object is a shallow clone. It uses native code because there are several restrictions that even using reflection can't be got around, but there are no such limitations in native languages (namely, C/C++). The two main restrictions I can think of is assigning to final fields and creating an arbitrary object class (mostly if the class doesn't provide a default constructor). There might be more.

    This is more than enough information which most Java developers won't need to deal with. If you are curious about the actual implementation, download the Java source and take a look.

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

    sravya (August 26th, 2013)

  6. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: clone method in java

    But without calling the constructor how does it create an object? if it calls the constructor why calling clone() in the above example (CloningEx ce1 = (CloningEx)ce.clone() ; ) did not print the sysout present in constructor?

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: clone method in java

    Objects are just bytes in memory. The object size must be known, so the native compiler will simply allocate the appropriate memory and place it into the Java heap. All it has to do for a shallow copy is to then copy the bytes from the original object to the new object, therefore no constructor is actually called.

    This doesn't work all of the time, which is why it is highly recommended that you override clone() with your own implementation which may involve calling a specific constructor.

  8. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: clone method in java

    Thank You so much!!

Similar Threads

  1. I can't clone an object
    By Dr. Eric Vornoff in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 21st, 2013, 11:29 PM
  2. How to clone a singleton class?
    By Pratyush in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 4th, 2013, 02:27 AM
  3. Making an Alchemy/Doodle God clone in Java
    By sapientij in forum Java Theory & Questions
    Replies: 1
    Last Post: October 29th, 2012, 07:42 AM
  4. Object.clone()
    By bbr201 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 14th, 2010, 10:55 AM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM