The Artima Developer Community
Sponsored Link

Don't need a copy constructor

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Don't need a copy constructor

Posted by Bill Venners on 18 Oct 1998, 9:26 AM

> I believe that all classes should support a copy constructor:

> public void someMethod (Foo fIsaFoo) {
> Foo barIsAnotherFoo = new Foo(fIsaFoo);
> ....
> }

> At the very least, a constructor with the signature:

> public Foo (Foo fooToCopy);

> should clone the argument supplied.


While I would agree that a C++ canonical object would have
a copy constructor, the clone() method is Java's way of
accomplishing the same thing. And clone() is already a
part of the canonical object idiom.

I have a bit of material about clone() versus the copy
constructor at the end of my Inner Java chapter on
cloning:


http://www.artima.com/innerjava/webuscript/cloning.html

See the section, "Clone and the Copy Constructor."

bv



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us