# Using constants

**URL:** https://discourse.processing.org/t/using-constants/44955
**Category:** Beginners
**Created:** [August 18, 2024, 6:09am UTC](https://discourse.processing.org/t/using-constants/44955 "2024-08-18T06:09:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Erik](https://avatars.discourse-cdn.com/v4/letter/e/b5ac83/32.png) [@Erik](https://discourse.processing.org/u/Erik)
#### Post date: [August 18, 2024, 6:09am UTC](https://discourse.processing.org/t/using-constants/44955/1 "2024-08-18T06:09:31Z")

</div>

I want to use constants in my projects, i.e. final values that never changes throughout the code, that can be accessed anywhere. What is the proper way of using constants in processing?

---

<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 18, 2024, 10:04am UTC](https://discourse.processing.org/t/using-constants/44955/2 "2024-08-18T10:04:29Z")

</div>

Whenever we declare variables outside of any function or {} blocks, they become global variables.

We can also add keywords `static` & `final` if they’re truly immutable; or at least if they’re supposed to be kept unchanged during the execution of the program.

We should also follow the SCREAMING\_SNAKE\_CASE naming convention for them.

We can define global variables inside any “.pde” tab, and they become available on other “.pde” files within the same sketch.

Also, if we have too many global variables, we can group them inside `enum`, `interface` or `class` structures as `static` fields.

Those structures will function as container namespaces for those constants.

---

<div class="post-metadata">

### Author: ![Erik](https://avatars.discourse-cdn.com/v4/letter/e/b5ac83/32.png) [@Erik](https://discourse.processing.org/u/Erik)
#### Post date: [August 18, 2024, 12:57pm UTC](https://discourse.processing.org/t/using-constants/44955/3 "2024-08-18T12:57:31Z")

</div>

Thanks for your reply.

When should I choose to make constants global, encapsulate them in a class structure, or use an enum class?

---

<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 18, 2024, 8:43pm UTC](https://discourse.processing.org/t/using-constants/44955/4 "2024-08-18T20:43:21Z")

</div>

> [@Erik](#):
>
> When should I choose to make constants global,

Contrary to global variables, which we should be more strict about it, we can pretty much abuse global constants, b/c they don’t keep state and have a permanent fixed value, which will always be the same during the execution of our code.

Here’s a sketch example using lotsa global constants:

> **[Sierpinski Pentagon \[Java\] - GoToLoop - OpenProcessing](https://openprocessing.org/sketch/955443)**

> [@Erik](#):
>
> , encapsulate them in a class structure,

If we have immutable values that are intimately related to a class or a group of classes, it makes total sense to define them as `static final` fields inside that class.

However, only primitive and string fields can be declared `static` inside an inner class.

As a workaround, we can define those constants inside an `interface`, and have the class or a group of related classes to `implements` that `interface`:

> **[implements / Reference](https://processing.org/reference/implements.html)**
>
> Implements an interface or group of interfaces. Interfaces are used to establish a protocol between classes; they establish the form for a class (method names, return types, etc.) but no…

> **[Players Movement (Java) - GoToLoop - OpenProcessing](https://openprocessing.org/sketch/718355)**
>
> https://Reddit.com/r/processing/comments/bshqa8/how\_would\_i\_make\_a\_keypressed\_function\_support/eor5epo/

```auto
interface IPlayer {
  static final color OUTLINE = 0;
  static final float BOLD = 2;
  static final int MODE = CENTER;
  static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;
}

class Player implements IPlayer {
  int x, y, d, v;
  color ink;
  byte[] keys;

  Player(int x, int y, int d, int v, color ink, byte[] keys) {
    this.x = x;
    this.y = y;
    this.d = d;
    this.v = v;
    this.ink = ink;
    this.keys = keys;
  }

  Player display() {
    fill(ink);
    ellipse(x, y, d, d);
    return this;
  }

  Player move() {
    final int
      r = d >> 1, 
      hor = keysDown[keys[RIGHT]] - keysDown[keys[LEFT]], 
      ver = keysDown[keys[DOWN]] - keysDown[keys[UP]];

    x = constrain(x + v*hor, r, width - r);
    y = constrain(y + v*ver, r, height - r);

    return this;
  }
}

```

> [@Erik](#):
>
> , or use an enum class?

An `enum` is a structure where its fields are themselves values.  
It’s a very good choice when we need to represent different, but related states.  
The `enum` name will become the namespace for its values.

As an example, we can group these 4 `static final` related fields from the previous code:  
`static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;`

Inside an `enum` structure:

```auto
static enum Direction {
  UP, DOWN, LEFT, RIGHT;
}

```
