How do u return a new object; an exact copy of the object which it is called?

Discussion in 'Java' started by Jazz, Oct 28, 2003.

  1. Jazz

    Jazz Guest

    Hello All,

    I was wondering, How do u return an exact copy of an object (Plate)
    which it is called upon.

    IE.

    In the Main:

    Plate p = new Plate();
    Plate p2 = p.getCopy();

    In the Class:
    public Plate getCopy() {
    ???
    }

    What do i do for..???

    i know ur prolly saying u can just go Plate p2 = p1, but I have my
    reasons :p

    Thanks

    Jazz
     
    Jazz, Oct 28, 2003
    #1
    1. Advertising

  2. Jazz <> scribbled the following:
    > Hello All,


    > I was wondering, How do u return an exact copy of an object (Plate)
    > which it is called upon.


    > IE.


    > In the Main:


    > Plate p = new Plate();
    > Plate p2 = p.getCopy();


    > In the Class:
    > public Plate getCopy() {
    > ???
    > }


    > What do i do for..???


    > i know ur prolly saying u can just go Plate p2 = p1, but I have my
    > reasons :p


    You will need to set every field of p2 to the same value as the field in
    p. If there are object fields, then you need to decide whether you want
    a shallow copy or a deep copy.
    In a shallow copy, just copy the references. p and p2 will then include
    references to the same objects. In a deep copy, make identical copies
    of the objects, and copy references to those. p and p2 will then
    include references to separate, but identical, objects.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To doo bee doo bee doo."
    - Frank Sinatra
     
    Joona I Palaste, Oct 28, 2003
    #2
    1. Advertising

  3. Re: How do u return a new object; an exact copy of the object whichit is called?

    Jazz wrote:
    > Hello All,
    >
    > I was wondering, How do u return an exact copy of an object (Plate)
    > which it is called upon.
    >
    > IE.
    >
    > In the Main:
    >
    > Plate p = new Plate();
    > Plate p2 = p.getCopy();
    >
    > In the Class:
    > public Plate getCopy() {
    > ???
    > }
    >
    > What do i do for..???


    Use the clone() method provided by Object. Implement Cloneable and

    public Plate getCopy() {
    return (Plate) super.clone();
    }

    Note that this will produce a "shallow copy".

    > i know ur prolly saying u can just go Plate p2 = p1,


    No we're not, because that wouldn't be a copy.
     
    Michael Borgwardt, Oct 28, 2003
    #3
  4. On Tue, 28 Oct 2003 09:48:37 -0800, Jazz wrote:

    > Hello All,
    >
    > I was wondering, How do u return an exact copy of an object (Plate)
    > which it is called upon.
    >
    > IE.
    >
    > In the Main:
    >
    > Plate p = new Plate();
    > Plate p2 = p.getCopy();
    >
    > In the Class:
    > public Plate getCopy() {
    > ???
    > }
    >
    > What do i do for..???
    >
    > i know ur prolly saying u can just go Plate p2 = p1, but I have my
    > reasons :p
    >
    > Thanks
    >
    > Jazz


    Hi Jazz,

    Java does not support cloning of object at compile time since it is
    uncertain whether or not a field object is a part-of or reference-of
    object (field primitives are easy since that is just a copy of the actual value).
    This means you have to specify youself what you mean by cloning.
    See http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Cloneable.html.

    Best Regards
    Kristian
     
    Kristian Bisgaard Lassen, Oct 28, 2003
    #4
    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. Apricot
    Replies:
    4
    Views:
    530
    velthuijsen
    Apr 16, 2004
  2. Els
    Replies:
    12
    Views:
    548
  3. Mark Sullivan
    Replies:
    0
    Views:
    269
    Mark Sullivan
    Dec 21, 2007
  4. debianna
    Replies:
    1
    Views:
    402
    richard
    Mar 14, 2009
  5. Replies:
    2
    Views:
    209
    Eric Bohlman
    Mar 6, 2005
Loading...

Share This Page