# Processing bug - setting A (object) to B (object) and modifying A, it will change B too!

**URL:** <https://discourse.processing.org/t/processing-bug-setting-a-object-to-b-object-and-modifying-a-it-will-change-b-too/31627>\
**Category:** Beginners\
**Created:** [August 8, 2021, 7:43pm UTC](https://discourse.processing.org/t/processing-bug-setting-a-object-to-b-object-and-modifying-a-it-will-change-b-too/31627 "2021-08-08T19:43:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [August 8, 2021, 7:43pm UTC](https://discourse.processing.org/t/processing-bug-setting-a-object-to-b-object-and-modifying-a-it-will-change-b-too/31627/1 "2021-08-08T19:43:35Z")

</div>

Hello everyone!  
There is an issue with PVectors and objects.  
If you do this:

```auto
PVector a = new PVector(0,0);
PVector b = new PVector(100,100);
PVector c = a;
c.lerp(b,0.5);
println(a,c); //prints [50.0, 50.0, 0.0] [50.0, 50.0, 0.0]

```

lerp will effect both a & c. The same happens with objects. Is this a bug or a feature. Feels more like a bug. Thank you!

EDIT: I know objects are saved as strange letter sequences but still

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 8, 2021, 7:51pm UTC](https://discourse.processing.org/t/processing-bug-setting-a-object-to-b-object-and-modifying-a-it-will-change-b-too/31627/2 "2021-08-08T19:51:20Z")

</div>

> [@CodeMasterX](#):
>
> `PVector c = a;`

In the case above variable _c_ is assigned w/ the same reference (memory address) value as variable _a_.

Therefore they’re both alias to the same PVector object.

If you don’t want _c_ to be an alias clone _a_ before assigning it to _c_ using method **copy()** or **get()**:  
`PVector c = a.get();`

> **[copy() / Reference](https://processing.org/reference/PVector_copy_.html)**
>
> Copies the components of the vector and returns the result as a PVector.

---

<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:** [August 8, 2021, 8:42pm UTC](https://discourse.processing.org/t/processing-bug-setting-a-object-to-b-object-and-modifying-a-it-will-change-b-too/31627/3 "2021-08-08T20:42:04Z")

</div>

PVector is inbuilt

But when I have my own class

Do i have to program a own copy function or is there  
an automatical function?

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [August 8, 2021, 9:07pm UTC](https://discourse.processing.org/t/processing-bug-setting-a-object-to-b-object-and-modifying-a-it-will-change-b-too/31627/4 "2021-08-08T21:07:31Z")

</div>

It’s an interesting question, @Chrisir, with a complicated answer. If your class contains references to other classes, or attributes that should be unique to each instance, you will need to define what constitutes a clone for that class, and then implement it as code.

For example, suppose you define a `Car` class. If you copy an instance of a `Car`, should all its attributes be identical, or should you give the copy its own vehicle identification number?

You will also need to decide whether if the `Car` was made in a `Factory`, you need to copy the `Factory` instance, as opposed to referencing the same one associated with the original `Car` instance.

_EDIT (August 8, 2021):_

For an overview of the issues, see [Wikipedia: Object copying](https://en.wikipedia.org/wiki/Object_copying). Then do some Googling. There are deep copies, shallow copies, and combinations.

Getting back the `Car` example, suppose we also have a `Dealer` class. The `Car` instance was bought at a `Dealer` instance. Every so often, the `Car` drives itself to the `Dealer` to get serviced.

A copy of the `Car` should probably share the same `Dealer` instance reference as the original, rather than have its own copy of the `Dealer`. Let’s say the `Dealer` instance reconfigures its facility, so that the `Car`, which is self-driving, needs to drive itself to a different garage door for future servicing. With all copies of the `Car` referencing the same `Dealer` instance, a change to that single `Dealer` can communicate the change to all pertinent `Car` instances. This would be better than having multiple copies of the `Dealer` instance, and is an aspect of making a shallow copy of the `Car`. In actuality, copying a `Car` would likely involve a combination of deep and shallow copying. Real cars share real dealers, but each one gets its own engine.
