Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Are you sure, Mr. Gosling? Date: Sat, 4 Jun 2016 16:43:35 -0400 Organization: A noiseless patient Spider Lines: 69 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 4 Jun 2016 20:43:41 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="9664"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pM7yeMILyrW9Z700MiYLR" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 In-Reply-To: Cancel-Lock: sha1:2fV0HUOJjrY0XC0DNIeEg9C/1T0= Xref: csiph.com comp.lang.java.programmer:34473 On 6/4/2016 3:27 PM, Arne Vajhøj wrote: > On 6/4/2016 2:01 PM, Stefan Ram wrote: >> There is this famous book »The Java Programming Language, >> Fourth Edition« by James Gosling et al. >> >> In it, it is written about the »idiom of copy constructor«: >> >> »This idiom is not used much within the Java class >> libraries, because the preferred way to make a direct >> copy of an object is by using the clone method.« >> >> Are you sure about this, Mr. Gosling? Why ask here? Do you think he reads this newsgroup? > I do not remember ever to have written a class with a constructor > with a single argument of same type. I've done it, but not very often. Usually it's when the class is mutable (for what seem to be good reasons) but I need an instance that won't be mutated: class MyClass { private final List things = new ArrayList<>(); public void rememberThing(Thing thing) { // Don't let the Thing change after I've remembered it. things.add(new Thing(thing)); } public Thing getThingAtIndex(int index) { // Don't expose a remembered Thing to possible change. return new Thing(things.get(index)); } ... } > And the only example that I can remember having seen > is java.lang.String. And usage of that is usually a code > smell. Well, the JavaDoc suggests that any class implementing Collection ought to provide a constructor that takes a Collection argument. So we could *almost* say that ArrayList has a constructor that takes a (slightly disguised) ArrayList as its argument. Similar remarks apply to Map. FWIW, Joshua Bloch appears to disagree with Gosling et al. From "Effective Java:" Is all this complexity [of implementing Cloneable] really necessary? Rarely. [...] you are probably better off providing some other means of object copying or simply not providing the capability. [emphasis his] A fine approach to object copying is to provide a copy constructor. [...] [emphasis and italics his] The copy constructor approach and its static factory variant have many advantages over Cloneable/clone [...] So: Who ya gonna trust? The guy who thought unsigned integers were "too complex" or the guy who invented collections? -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM