what is the use of clone method

Discussion in 'Java' started by Ravi, Jan 22, 2008.

  1. Ravi

    Ravi Guest

    I have java api docs, but still I am not able to understand the use of
    clone() method provided by the Object class.

    Can You please explain.
    Ravi, Jan 22, 2008
    #1
    1. Advertising

  2. Ravi <> writes:

    > I have java api docs, but still I am not able to understand the use of
    > clone() method provided by the Object class.
    >
    > Can You please explain.


    The purpose of the clone method is to create an identical copy of
    an object.

    An identical copy means an instance of the same class with the same
    values for all fields. Reference fields will point to the same objects,
    primitive type fields will have the same value.

    This copy is created without calling any constructors or other
    initialization code on the object. This is somewhat acceptable, since
    it cannot create an object in an uninitialized state (like the "new"
    operator does before the constructors are executed).

    If there are further requirements on instances of a class that are
    not satisfied if you create an identical copy, then you should not
    allow cloning.

    Cloning is only allowed for classes that implement the marker
    interface java.lang.Clonable, and which expose the clone() method
    (which is protected on Object).

    The typical way to use cloning is to create a class:

    public class MyClass implements Clonable {
    // my fields and methods ...

    // make a public clone()
    public Object clone() {
    return super.clone();
    }
    }

    If you need to change the behavior of the clone method, you can
    change the cloned copy in your own clone method, e.g., cloning
    some referenced objects too instead of referencing the same objects.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 22, 2008
    #2
    1. Advertising

  3. Ravi

    Roedy Green Guest

    On Tue, 22 Jan 2008 08:31:19 -0800 (PST), Ravi <>
    wrote, quoted or indirectly quoted someone who said :

    >I have java api docs, but still I am not able to understand the use of
    >clone() method provided by the Object class.


    see http://mindprod.com/jgloss/clone.html

    Cloning only makes sense for mutable objects. Instead of construction
    a new one from scratch, you copy an existing exemplar all
    pre-initialised.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
    Roedy Green, Jan 22, 2008
    #3
  4. To copy the object. Lets say that you write a class that contains an
    object that you want to give to te user, but you don't want them to
    mess with it. They can do this because when you pass an object what
    is passed is a refrence to that object, but the purpose of the clone
    is to gen a copy that is not related to that object it clones.
    Chase Preuninger, Jan 23, 2008
    #4
  5. Ravi

    Lew Guest

    Chase Preuninger wrote:
    > ...the purpose of the clone
    > is to gen a copy that is not related to that object it clones.


    Actually, unless you implement clone() to do a deep copy, the clone could very
    well be tightly coupled to the original object.

    --
    Lew
    Lew, Jan 23, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Walter Mitty

    Object clone method & inheritance

    Walter Mitty, Jul 14, 2003, in forum: Java
    Replies:
    0
    Views:
    351
    Walter Mitty
    Jul 14, 2003
  2. Osaman
    Replies:
    2
    Views:
    428
    John C. Bollinger
    Jan 5, 2004
  3. Andrew Mallinson

    The clone() method

    Andrew Mallinson, Feb 3, 2004, in forum: Java
    Replies:
    3
    Views:
    3,178
    Mike Schilling
    Feb 4, 2004
  4. michael

    Clone() method SDK 1.4

    michael, May 7, 2004, in forum: Java
    Replies:
    4
    Views:
    1,325
    Roedy Green
    May 10, 2004
  5. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    703
    lordy
    Jul 7, 2006
Loading...

Share This Page