# Two PVector variables update but only one is changed

**URL:** <https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388>\
**Category:** Coding Questions\
**Created:** [January 10, 2019, 2:42pm UTC](https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388 "2019-01-10T14:42:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![delsey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/delsey/32/11380_2.png) [@delsey](https://discourse.processing.org/u/delsey)\
**Post date:** [January 10, 2019, 2:42pm UTC](https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388/1 "2019-01-10T14:42:29Z")

</div>

```auto
PVector p;
PVector newP;

void setup() {
  p = new PVector(500, 300);
  newP = new PVector(500, 300);
}

void draw() {
  newP = p;
  println(newP + " < newP before");

  // Adds to p
  p.add(50, 100);

  println(newP + " < newP after");
  println(p + " < p");
  println();
  delay(5000);
}

```

When I .add to p, the variable newP changes aswell. Am I missing something here? Does the .add() method do something else than I thought it would? Can someone explain this behavior?

Result:

```auto
[500.0, 300.0, 0.0] < newP before
[550.0, 400.0, 0.0] < newP after
[550.0, 400.0, 0.0] < p

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 10, 2019, 3:09pm UTC](https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388/2 "2019-01-10T15:09:08Z")

</div>

the problem is not the ADD,

> //p2 = p1;  
> [500.0, 300.0, 0.0] \< p1 before  
> [500.0, 300.0, 0.0] \< p2 before  
> [550.0, 400.0, 0.0] \< p1 after add  
> [500.0, 300.0, 0.0] \< p2 after add  
> // but if use  
> p2 = p1;  
> [500.0, 300.0, 0.0] \< p1 before  
> [500.0, 300.0, 0.0] \< p2 before  
> [550.0, 400.0, 0.0] \< p1 after add  
> [550.0, 400.0, 0.0] \< p2 after add

but if you use

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

> p2 = p1.copy();  
> [500.0, 300.0, 0.0] \< p1 before  
> [500.0, 300.0, 0.0] \< p2 before  
> [550.0, 400.0, 0.0] \< p1 after add  
> [500.0, 300.0, 0.0] \< p2 after add

---

<div class="post-metadata">

**Author:** ![delsey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/delsey/32/11380_2.png) [@delsey](https://discourse.processing.org/u/delsey)\
**Post date:** [January 10, 2019, 3:15pm UTC](https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388/3 "2019-01-10T15:15:13Z")

</div>

Oh. So its an attribute of pvectors to do that. I still dont think its logical but that does the trick. Thanks.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 10, 2019, 3:17pm UTC](https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388/4 "2019-01-10T15:17:09Z")

</div>

> [@delsey](#):
>
> So its an attribute of pvectors to do that

actually it is the way “=” works on objects ( it is referenced not set to equal values )

---

<div class="post-metadata">

**Author:** ![delsey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/delsey/32/11380_2.png) [@delsey](https://discourse.processing.org/u/delsey)\
**Post date:** [January 10, 2019, 3:22pm UTC](https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388/5 "2019-01-10T15:22:31Z")

</div>

Youre right. I should have known that. Ive been programming for quite some time now, strange that this has never caused me problems like that.

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 10, 2019, 3:24pm UTC](https://discourse.processing.org/t/two-pvector-variables-update-but-only-one-is-changed/7388/6 "2019-01-10T15:24:05Z")

</div>

Yeah, these are objects. `p` and `newP` are only pointers to these objects. So, if you do `newP = p;`, it’s setting both of these pointers to the same object, and, as a result,since any manipulations directed at the pointer happen on the object that it’s pointing at, it’s getting reflected in any pointers of that object.

Object oriented programming! 😃
