How to use static variables in Processing?

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:

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

Gives me this error:

The local variable a may not have been initialized

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;

1 Like

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:

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

When I change it to private I get this error:

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:

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

But output window freezes and I get error this error:

NullPointerException

Hi

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

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


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.

1 Like

This is good start to understand function

https://funprogramming.org/31-Function-parameters-and-return-values.html

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

How to use a static variable from inside a function ?

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

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

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

void draw(){
  
};
4 Likes

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:

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.

1 Like