Clone
From Abap2Java
[edit]
ABAP
There is no built-in clone method in ABAP, and there is no built-in interface called clonable. However, every class can easily implement its own clone method:
CLASS MyClass DEFINITION. ... METHODS clone RETURNING rr_object TYPE REF TO MyClass. ENDCLASS. CLASS MyClass IMPLEMENTATION. METHOD clone. CREATE OBJECT rr_object. "copy all attributes from me to rr_objects ENDMETHOD. ENDCLASS.
See also:
CREATE OBJECT, INTERFACE
[edit]
Java
Since every object variable contains only a reference to an object, the following does not, what you propably expect:
// creates *only* copy of reference. objA and obj still point to the identical object objA = obj; // creates copy of object. References do not point to the identical object anymore objB = obj.clone();
Be aware that the clone method only creates a flat bitwise copy. If the object contains fields containing objects, they won't be cloned. Instead clone deliveres the same references to those objects. Nethertheless cloning an object makes sure both variables do not point to the same object.
Custumizing the clone() method:
class MyClass implements Cloneable{ public Object clone() throws CloneNotSupportedException { Object obj = super.clone(); // do custom cloning return obj; } }
See also:
External links:
Java JDK 1.4.2 Object
Creating Object Wikibooks Java Programming

