# Shared PVector values in Classes

**URL:** <https://discourse.processing.org/t/shared-pvector-values-in-classes/3662>\
**Category:** Coding Questions\
**Created:** [September 19, 2018, 8:07am UTC](https://discourse.processing.org/t/shared-pvector-values-in-classes/3662 "2018-09-19T08:07:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blosh](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@blosh](https://discourse.processing.org/u/blosh)\
**Post date:** [September 19, 2018, 8:07am UTC](https://discourse.processing.org/t/shared-pvector-values-in-classes/3662/1 "2018-09-19T08:07:21Z")

</div>

I have created a new Class, “Polygon” that uses a PVector to store the position on the screen that a shape is displayed. However, whenever I update the position of one the Polygons it updates the position of all the others, effectively making all Polygons centred around the same position. I know this has something to do with PVectors calling on the same object or something but I don’t properly understand it. Is there anyway around this problem or should I just store the x and y position as 2 separate floats?

---

<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:** [September 19, 2018, 8:11am UTC](https://discourse.processing.org/t/shared-pvector-values-in-classes/3662/2 "2018-09-19T08:11:31Z")

</div>

[Processing.org/reference/PVector\_set\_.html](http://Processing.org/reference/PVector_set_.html)

```auto
class Polygon {
  final PVector vec = new PVector();

  Polygon(final PVector v) {
    vec.set(v);
  }
}

```

---

<div class="post-metadata">

**Author:** ![blosh](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@blosh](https://discourse.processing.org/u/blosh)\
**Post date:** [September 19, 2018, 8:25am UTC](https://discourse.processing.org/t/shared-pvector-values-in-classes/3662/3 "2018-09-19T08:25:53Z")

</div>

What does ‘final’ do? I’ve done ‘vec = v;’ rather than using ‘vec.set(v)’ because for some reason ‘vec.set(v)’ causes an error for some reason, but that shouldn’t make a difference right? But my issue is that when something like this is run:

`PVector position = new PVector(200, 200); Polygon square = new Polygon(position); Polygon pentagon = new Polygon(position); pentagon.vec.x = 400; println(square.vec.x);`  
This prints 400 because line 4 updates the x position for all Polygons. When I want it to print 200.

---

<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:** [September 19, 2018, 8:34am UTC](https://discourse.processing.org/t/shared-pvector-values-in-classes/3662/4 "2018-09-19T08:34:59Z")

</div>

> [@blosh](#):
>
> What does `final` do?

> **[final / Reference](https://processing.org/reference/final.html)**
>
> Keyword used to state that a value, class, or method can't be changed. If the final keyword is used to define a variable, the variable can't be changed within the program. When used to define a…

> [@blosh](#):
>
> I’ve done `vec = v;` rather than using `vec.set(v)` because for some reason `vec.set(v)` causes an error for some reason, but that shouldn’t make a difference right?

```auto
void setup() {
  final PVector pos = new PVector(200, 200);

  final Polygon square = new Polygon(pos); 
  final Polygon pentagon = new Polygon(pos);

  pentagon.vec.x = 400; 
  println(square.vec.x); // 200.0

  exit();
}

class Polygon {
  final PVector vec = new PVector();

  Polygon(final PVector v) {
    vec.set(v);
  }
}

```
