# How to lock a variable?

**URL:** https://discourse.processing.org/t/how-to-lock-a-variable/32515
**Category:** Beginners
**Created:** [September 29, 2021, 7:55pm UTC](https://discourse.processing.org/t/how-to-lock-a-variable/32515 "2021-09-29T19:55:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 29, 2021, 7:55pm UTC](https://discourse.processing.org/t/how-to-lock-a-variable/32515/1 "2021-09-29T19:55:35Z")

</div>

how to make a variable unchangeable so it can’t be changed anymore?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [September 29, 2021, 8:06pm UTC](https://discourse.processing.org/t/how-to-lock-a-variable/32515/2 "2021-09-29T20:06:26Z")

</div>

> **[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…

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 29, 2021, 8:07pm UTC](https://discourse.processing.org/t/how-to-lock-a-variable/32515/3 "2021-09-29T20:07:51Z")

</div>

i tried this but it doesn’t work, does it not work on pvectors or is it because it was in the middle of my code instead of at the top?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [September 29, 2021, 8:11pm UTC](https://discourse.processing.org/t/how-to-lock-a-variable/32515/4 "2021-09-29T20:11:17Z")

</div>

Not sure though it might not make sense to allow final with pvectors considering all the functionality pvectors have.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [September 29, 2021, 8:12pm UTC](https://discourse.processing.org/t/how-to-lock-a-variable/32515/5 "2021-09-29T20:12:26Z")

</div>

Please note that

`This keyword is an essential part of Java programming and is not usually used with Processing. Consult a Java language reference or tutorial for more information.`

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [September 29, 2021, 8:57pm UTC](https://discourse.processing.org/t/how-to-lock-a-variable/32515/6 "2021-09-29T20:57:33Z")

</div>

`final` works great with primitive variables like `int` `float` and `boolean`. If you save a `final float x` and `final float y`, that is all you need.

PVector is an object with internal variables x, y, z. You can create a final PVector variable and only one object can be assigned to it – you can’t assign a new object to that variable – but that doesn’t mean the object itself cannot be changed internally.

The PVector is final, but its x, y, z are not final.

```auto
final PVector foo = new PVector(10,10);
foo = new PVector(20,20);

```

Once you use `final` the second line is illegal and show the error message:

> The final local variable foo cannot be assigned.

However, being final doesn’t make it immutable.

```auto
final PVector foo = new PVector(10,10);
foo.rotate(PI);
println(foo);

```

> [-9.999999, -10.000001, 0.0]

```auto
foo.x = 123;

```

> [123.0, -10.000001, 0.0]

If you have PVectors and you don’t want your code to accidentally change them, there are several ways to approach it:

1. The easiest thing – don’t call mutator methods on your PVectors.
2. Create a class like a PVector, `class MyVector`, with final x, y, z and only the methods that you want – add, or rotate.
3. Extend PVector and make a class that with a final x, y, z. This gets a bit tricky with how you handle overriding, but it can be demonstrated – it just usually isn’t worth it. In particular, in addition to overriding methods like add, rotate, etc., you’ll want to override toString so that the class doesn’t print 0,0,0, the values of its shadowed variables.
4. Don’t: Extend PVector and make a class that overrides every mutator method in order to disable them. This doesn’t really work well with PVector because if x and y are directly exposed, so you can always pv.x = 123. To be safe, use method 3 instead.
5. Wrap a PVector inside a new class, e.g. VecWrapper, which has a private PVector variable and only lets you create it or read it but not change it.

You may also be interested in the idea of unmodifiable – an interface that doesn’t provide ways of changing a thing – vs immutable – a guarantee that nothing can change a thing.

> <https://stackoverflow.com/questions/8892350/immutable-vs-unmodifiable-collection/8892376#8892376>

The solutions above are either to make your own immutable thing, or to extend it or wrap a PVector in ways that make it unmodifiable (even though, inside, it can still be changed).

Java objects aren’t really great at being deeply immutable unless they were first created that way by design to have things made of things made of things none of which can change. So often, even if you make a static or final collection of stuff, there may be changeable stuff inside it. The list can’t change, but the parts can.
