Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34564
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2016-06-08 06:31 -0700 |
| References | (8 earlier) <drm6bcFhke1U1@mid.individual.net> <5c864ac4-9634-4c88-9d6b-d14dbf3b3886@googlegroups.com> <drodg0FejbU1@mid.individual.net> <746532a2-c6db-4d58-afc0-1cc57440eff5@googlegroups.com> <nj7asv$j9j$1@dont-email.me> |
| Message-ID | <9ecc34ba-fac1-45ed-88cb-71403c7c906d@googlegroups.com> (permalink) |
| Subject | Re: Are you sure, Mr. Gosling? |
| From | Eric Douglas <e.d.programmer@gmail.com> |
On Tuesday, June 7, 2016 at 4:30:34 PM UTC-4, Mike Amling wrote:
> If you need obj1, obj2 and obj3 to themselves be cloned rather than just
> copied, it gets more complicated.
By cloned you mean copied by value and by copied you mean copied by reference?
A copy constructor has the same amount of runtime/memory overhead as a clone method?
If both methods return the same object, my understanding is the copy constructor just looks cleaner if you have a lot of variables you want as default values and the clone method will copy those by default so should look cleaner if you have a lot of variables you want copied.
A copy constructor by default creates an instance of the current class. A clone method by default creates an instance of Object, so either the clone method of the Cloneable class must cast super.clone() to it's own class, or everything that calls it's clone method must cast the result.
Defaults from clone() to consider (if we don't want these, we either need to specify the alternative in the copy constructor, or replace in the clone method on the new instance returned from super.clone()): Immutable objects get copied by value, bucket objects get copied by reference.
Running the following class:
import java.util.ArrayList;
public abstract class TestStatic {
public static void main(final String[] args) {
final BigCat c1 = new BigCat();
c1.setVar1(7);
c1.setVar2(5);
final ArrayList test = new ArrayList();
test.add("one");
c1.setA1(test);
final Cat c2 = c1.clone();
c1.setVar1(9);
System.out.println(c2.getVar1());
// doesn't work unless clone result is cast to BigCat
// System.out.println(c2.getVar2());
test.add("two");
System.out.println(c2.getA1());
// returns a BigCat without cast
System.out.println(c2.getClass());
}
public static class Cat implements Cloneable {
private Integer var1 = 1;
private ArrayList a1 = null;
@Override
public Cat clone() {
try {
return (Cat) super.clone();
} catch (final CloneNotSupportedException c) {
// Not likely.
throw new IllegalStateException();
}
}
ArrayList getA1() {
return a1;
}
void setA1(final ArrayList newArray) {
a1 = newArray;
}
Integer getVar1() {
return var1;
}
void setVar1(final Integer newVar) {
var1 = newVar;
}
}
public static class BigCat extends Cat {
private Integer var2 = 1;
Integer getVar2() {
return var2;
}
void setVar2(final Integer newVar) {
var2 = newVar;
}
}
}
gets the following output:
7
[one, two]
class com.blockhouse.javatest.utils.TestStatic$BigCat
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 15:27 -0400
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-04 16:43 -0400
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 18:44 -0400
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-05 08:05 -0400
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-05 08:31 -0400
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-06 05:22 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-06 09:00 -0400
Re: Are you sure, Mr. Gosling? v_borchert@despammed.com (Volker Borchert) - 2016-06-06 16:06 +0000
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-06 22:56 +0200
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 05:08 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 08:41 -0400
Re: Are you sure, Mr. Gosling? Martin Gregorie <martin@address-in-sig.invalid> - 2016-06-07 13:14 +0000
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 06:30 -0700
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 19:10 +0200
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 10:22 -0700
Re: Are you sure, Mr. Gosling? Mike Amling <mamling@chaff.us> - 2016-06-07 15:31 -0500
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 06:31 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 10:41 -0400
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 10:21 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 14:10 -0400
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 20:54 +0200
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 16:36 -0400
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 23:11 +0200
Re: Are you sure, Mr. Gosling? Lew <lewbloch@gmail.com> - 2016-06-10 12:20 -0700
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-14 19:10 +0200
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 17:12 -0400
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 23:25 +0200
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-07 17:33 -0400
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-06 21:18 -0400
csiph-web