The Artima Developer Community
Sponsored Link

Copy constructors



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.


Copy constructors

Posted by Martin Maisey on 12 Oct 1998, 7:55 AM

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

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

I'm not sure this is necessary - as far as I can see, a copy constructor doesn't provide any different semantics from clone(), so why provide it? It's not needed for by-value parameter passing as in C++, as this feature isn't supported in Java.

This is covered in Thinking in Java by Bruce Eckel, IMHO one of the best Java books out there. Read it for a better explanation than I could ever give.



Sponsored Links

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