Implementing Clonable in Java

Posted by Artium on Stack Overflow See other posts from Stack Overflow or by Artium
Published on 2010-04-25T22:42:38Z Indexed on 2010/04/25 22:53 UTC
Read the original article Hit count: 188

Filed under:
|
|
|

In which cases should I use this way:

public A clone() throws CloneNotSupportedException {

A clone = (A)super.clone(); clone.x= this.x; return clone; } And in which cases should I use that way:

public ShiftedStack clone() throws CloneNotSupportedException {

return new A(this.x); }

What should I do if x is final and I want to use the first way?

Regarding the first way, I understand it like this: we clone the super class and up-cast it, leading to some members uninitialized. After this initialize these members. Is my understanding correct?

Thank you.

© Stack Overflow or respective owner

Related posts about java

Related posts about clone