JDK-4230604 : clone() vs. Constructors taking own type
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.1.6,1.3.0
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic,windows_95
  • CPU: generic,x86
  • Submitted: 1999-04-16
  • Updated: 2008-05-22
  • Resolved: 2008-05-22
Related Reports
Relates :  
Description

Name: gb36485			Date: 04/16/99


I believe that this is a design flaw (or at least anomaly).

In C++ I use special (often protected or private) constructors when I want to create a clone for some special purpose. I'm using Cloneable in Java and overriding clone() when I need to further make copies of objects that my instance has a reference to. In following this paradigm, it would be great if I could just call clone() of every object that is copy-able. But it doesn't seem consistent.

For instance, the String class supports a constructor that looks like this:

String(String)

That's fine. But a the String class it's self does not implement Cloneable even though it is "copy-able". Perhaps there is something "by design" that I'm missing. Otherwise, in future versions of the JDK, perhaps some classes can have constructors that take a single parameter of their own type to make copies, but if they are "copy-able" at all they should all implement Cloneable (i.e., follow the same paradigm). Perhaps it would be more fitting to depreciate any existing constructors of this type and make all the existing (and new) JDK classes implement Cloneable if their instances can be copied.

Can you please reply to this message? 

Thanks,
(Review ID: 34922)
======================================================================

Comments
EVALUATION String is a poor motivating example. Normally one clones mutable objects so that both clones can mutate independently. But that cannot happen for String. The only way new String(s) differs from s is that new String(s) != s which has consequences for things like String.intern. Using String(String) is generally denigrated.
25-09-2007

EVALUATION Wholesale refactoring of java.lang and java.util to use Cloneable more widely is unlikely.
25-09-2007

EVALUATION I agree it seems inconsistent. I assume this is just history rather than design. I doubt if it's worth destabilizing things by deprecating copy constructors. I'm adding Josh to the interest list. This is the sort of thing he will have an opinion on. gilad.bracha@eng 1999-04-17
17-04-1999