Am I on the right track?
View. But not really. C ++ distinguishes between reassigning an existing object and creating a new one.
Java does not work. You cannot reassign an existing object in Java 1 (but you can, of course, reassign a link). In Java, to copy an object (rather than assign a reference to it), you usually use the copy constructor:
Class(Class other) { // Copy members of `other` into `this`. }
And then use it like this:
Class x = new Class(something here); Class y = new Class(x);
In particular, this is what all Java containers implement. I would not rely on clone . First of all, clone should be used only if the class implements the Cloneable tag Cloneable . Secondly, clone s design may be broken and its use is not recommended .
1 Well, you can, of course, reassign the members of the object (if they are not final ), and you could mimic the assignment operator of a C ++ copy by providing the assign to method. However, this is not an ordinary way of doing things in Java (although it may have its place in some exceptional cases).
Konrad Rudolph
source share