# How to use static variables in Processing?

**URL:** https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774
**Category:** Beginners
**Created:** [October 12, 2021, 4:34am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774 "2021-10-12T04:34:44Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![R1S8K](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@R1S8K](https://discourse.processing.org/u/R1S8K)
#### Post date: [October 12, 2021, 4:34am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/1 "2021-10-12T04:34:45Z")

</div>

Hi,

I’m working on a LCD128x64 C code for an Arduino platform and doing my quick snippets with processing.

Now, I need to use static variables inside a function, but apparently there are no static keyword in processing. So how to do it ?

I’m using this simple function trying to declare a static variable:

```auto
void print_static(){
 final int a;
 
 a+=1;
 println(a);
}

```

Gives me this error:

> The local variable a may not have been initialized

---

<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: [October 12, 2021, 5:02am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/2 "2021-10-12T05:02:13Z")

</div>

You add 1 to a. What is the value of a? You declared it, but didn’t initialize it, as the error says. Did you want a to be = 0?

final int a = 0;

---

<div class="post-metadata">

### Author: ![R1S8K](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@R1S8K](https://discourse.processing.org/u/R1S8K)
#### Post date: [October 12, 2021, 6:00am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/3 "2021-10-12T06:00:47Z")

</div>

I’m until now don’t know what is the actual use of `private` and `final`.

I think `final` is like `const` in C/C++. And `private` maybe like `static`.

This works fine for `final` and can’t be changed:

```auto
void print_static(){
  final int a = 100;
  println(a);
}

```

When I change it to `private` I get this error:

```auto
void print_static(){
  private int a;
  println(a);
}

```

Error:

> Illegal modifier for parameter a; only final is permitted

So I guess I have to use a `class` to be able to use `private`. But I’m having some problems calling functions from a class. I don’t know what I’m missing here. I tried this:

```auto
public class class_example {
  
  public void print_hi() {
    println("Hi there!");
  }
  
}

```

But output window freezes and I get error this error:

> NullPointerException

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 12, 2021, 7:06am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/4 "2021-10-12T07:06:10Z")

</div>

Hi

```auto
//public class class_example {
  
void setup(){
  size(200,200);
  print_hi();
}

  public void print_hi() {
    println("Hi there!");
  }
 

```

---

<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: [October 12, 2021, 7:52am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/5 "2021-10-12T07:52:59Z")

</div>

If you want to call a function from inside a class without instancing the class you have to make the class static and the function static.

Then you’ll use `class.function()` to call it.

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 12, 2021, 9:14am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/6 "2021-10-12T09:14:45Z")

</div>

This is good start to understand function

[https://funprogramming.org/31-Function-parameters-and-return-values.html](https://funprogramming.org/31-Function-parameters-and-return-values.html)

---

<div class="post-metadata">

### Author: ![R1S8K](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@R1S8K](https://discourse.processing.org/u/R1S8K)
#### Post date: [October 12, 2021, 11:38am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/7 "2021-10-12T11:38:33Z")

</div>

What about the static variable inside a function which is my main issue ?

How to use a static variable from inside a function ?

---

<div class="post-metadata">

### Author: ![R1S8K](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@R1S8K](https://discourse.processing.org/u/R1S8K)
#### Post date: [October 12, 2021, 11:40am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/8 "2021-10-12T11:40:05Z")

</div>

The tutorial is easy and doesn’t talk about static variables within a function.

---

<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: [October 12, 2021, 11:43am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/9 "2021-10-12T11:43:23Z")

</div>

```auto
static class ca{
  final static int num = 10;
  
  
  static int func(){
    return num;
  }
};

void setup(){
  println(ca.num);
  println(ca.func());
};

void draw(){
  
};

```

---

<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: [October 12, 2021, 3:35pm UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/10 "2021-10-12T15:35:09Z")

</div>

> [@R1S8K](#):
>
> , but apparently there is no static keyword in Processing.

> **[static / Reference](https://processing.org/reference/static.html)**
>
> Keyword used to define a variable as a "class variable" and a method as a "class method." When a variable is declared with the static keyword, all instances of that class share the same variabl…

> [@R1S8K](#):
>
> Now, I need to use static variables inside a function,

Even though Java (and other C-based languages like JavaScript) has a similar C syntax, not all C features are available; like pointer operators, static local variables, etc.

In Java we can have static fields, methods, classes; but neither local variables nor parameters can be declared static.

The general approach for Processing sketches is just declare global variables.

But if you prefer better separation of concerns you’re gonna need to create a class to place both the variables (fields) and their related functions (methods) grouped together as 1 structure, like @paulgoux already posted.

Here’s another example:

```auto
void setup() {
  frameRate(1);
}

void draw() {
  background((color) random(#000000));
  print(Value.nextValue(), TAB);
}

static class Value {
  static int a;

  static int nextValue() {
    return ++a;
  }
}

```

Basically the name of the class Value becomes the global “variable” now.

So it isn’t that much better than vanilla global variables; unless you need so many global variables that grouping them in classes is the sane thing to do.

---

<div class="post-metadata">

### Author: ![R1S8K](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@R1S8K](https://discourse.processing.org/u/R1S8K)
#### Post date: [October 26, 2021, 7:57pm UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/11 "2021-10-26T19:57:14Z")

</div>

Hi,

I think `final static int num = 10;` means it’s constant and can’t be changed.

I need a static variable.

And what does `final static` do ? It declares an integer to be what exactly ?

---

<div class="post-metadata">

### Author: ![R1S8K](https://avatars.discourse-cdn.com/v4/letter/r/3be4f8/32.png) [@R1S8K](https://discourse.processing.org/u/R1S8K)
#### Post date: [October 26, 2021, 8:10pm UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/12 "2021-10-26T20:10:11Z")

</div>

Hi,

I actually did a simple example and I think I learned something.

static works in global and inside class only and doesn’t work inside a local function.

This is my example:

```auto
static class var_specs{

  static int a = 9;
  
  
};

static int b = 90;

void setup(){
  
  println(var_specs.a);
  var_specs.a++;
  println(var_specs.a);
  
  //////////////////////////////////
  
  println(b);
  b++;
  println(b);
  
  
}

void local_function(){
 static int c = 88; // <---------------- here's the problem
 c++;
 println(c);
 
 // it issues this error:
 // --> Illegal modifier for parameter c; only final is permitted
  
}

```

Am I understanding it right ?

---

<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: [October 27, 2021, 12:42am UTC](https://discourse.processing.org/t/how-to-use-static-variables-in-processing/32774/13 "2021-10-27T00:42:47Z")

</div>

That’s correct, at least in reference to processing. I think standard java doesn’t have this limitation.

Final is the equivalent of const, and defines a variable as unchangeable. Removing final should cause no problem to your original request. Just note you can use final, private public and protected. Read more about these on the java ref if you want to find out more.
