Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do u return a new object; an exact copy of the object which it is called?

Reply
Thread Tools

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

 
 
Jazz
Guest
Posts: n/a
 
      10-28-2003
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

Thanks

Jazz
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-28-2003
Jazz <(E-Mail Removed)> 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


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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To doo bee doo bee doo."
- Frank Sinatra
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-28-2003
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.

 
Reply With Quote
 
Kristian Bisgaard Lassen
Guest
Posts: n/a
 
      10-28-2003
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
>
> 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/...Cloneable.html.

Best Regards
Kristian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a testsite exact copy of an ASP.NET2.0 site statichash Software 0 05-21-2009 03:37 PM
Copy the exact campaign that make up to $94,133 in 30 days debianna HTML 1 03-14-2009 01:55 AM
Difference between servers while code is exact copy (in IE) Els HTML 12 07-12-2006 11:25 AM
Please help - Exact Audio Copy can't detect my CD drives Lee Computer Information 3 04-16-2005 07:21 AM
Re: Make an exact copy of Hard Drive on 2nd Partition? william.moore7 Computer Support 1 09-12-2003 11:31 PM



Advertisments