# How to clone Object without modifying original?

**URL:** <https://discourse.processing.org/t/how-to-clone-object-without-modifying-original/28496>\
**Category:** Coding Questions\
**Created:** [March 13, 2021, 1:28pm UTC](https://discourse.processing.org/t/how-to-clone-object-without-modifying-original/28496 "2021-03-13T13:28:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![EinsMalteHH](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@EinsMalteHH](https://discourse.processing.org/u/EinsMalteHH)\
**Post date:** [March 13, 2021, 1:28pm UTC](https://discourse.processing.org/t/how-to-clone-object-without-modifying-original/28496/1 "2021-03-13T13:28:01Z")

</div>

I have an array of a class, that I want to clone. But if I modify the clone, the original gets modified to. Now is the question, how can i clone an array of classes without it modifying the original?

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 13, 2021, 2:25pm UTC](https://discourse.processing.org/t/how-to-clone-object-without-modifying-original/28496/2 "2021-03-13T14:25:51Z")

</div>

Hi! Unfortunately that’s how it works in Java (and JavaScript too). That’s why classes implemented in Processing has `copy()` function to clone the object often called deep copy.

As far as I understand, you need to implement it by yourself to deep copy the object. This is an example of PVector

> <https://github.com/processing/processing-android/blob/master/core/src/processing/core/PVector.java#L371>

I think this is because you can have another object inside the object, and in that case having a deep copy is complicated. Perhaps the best practice would be to implement your own `copy` function that copies to the extent of what you need, and iterate the function for all the elements in the array (make sure you first create a new array, not to copy it from the original one).

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2021, 8:30pm UTC](https://discourse.processing.org/t/how-to-clone-object-without-modifying-original/28496/3 "2021-03-13T20:30:01Z")

</div>

> [@micuat](#):
>
> to implement your own `copy` function

yeah, that would be something like

```auto
class ClassName {

....
....

ClassName copy () {
   ClassName newElement = new ClassName(); 

   newElement.x= x; 
   newElement.y= y; 
   newElement.col= col; 
   // copy all properties you need here

   return newElement;  
}

}
//

```

---

<div class="post-metadata">

**Author:** ![EinsMalteHH](https://avatars.discourse-cdn.com/v4/letter/e/ecd19e/32.png) [@EinsMalteHH](https://discourse.processing.org/u/EinsMalteHH)\
**Post date:** [March 13, 2021, 8:41pm UTC](https://discourse.processing.org/t/how-to-clone-object-without-modifying-original/28496/4 "2021-03-13T20:41:16Z")

</div>

Found a way to implement that already. Thx
