Not a question about code (you don't show any code...). Moved.
We rarely use static in Processing sketches, actually. Basically, using it on a global variable is quite useless, and using it in a class is roughly equivalent to making the variable global...
But the base effect in a class is to allow to share the value of the static variable (field, in Java terms) among all instances of a class.
An example of usage:
void setup()
{
ArrayList<Foo> list = new ArrayList<Foo>();
for (int i = 10; i > 0; i--)
{
list.add(new Foo("Foo " + i));
}
for (Foo f : list)
{
println(f);
}
exit();
}
static class Foo // In Processing, class must be static to use a static field
{
static int instanceinstanceCounterCounter;
int id;
String name;
Foo(String name)
{
id = ++instanceCounter;
this.name = name;
}
public String toString()
{
return name + " (" + id + "/" + instanceCounter + ")";
}
}
You can see that the value of instanceCounter is the same for all instances of Foo.
Static classes are quite different beasts, I won't enter in differences between inner and nested classes here.
Yes, I was talking about the real variable static fields, not about the constants, but I should have been clearer.
As said, static final fields in classes are less useful in Processing, but at least, they allow to scope them, and to avoid to pollute the global namespace!
Scope them: you can have a Button.WIDTH and a TextBox.WIDTH, for example, with different values.
Answers
Have you read its reference? http://processing.org/reference/static.html
Not a question about code (you don't show any code...). Moved.
We rarely use
static
in Processing sketches, actually. Basically, using it on a global variable is quite useless, and using it in a class is roughly equivalent to making the variable global...But the base effect in a class is to allow to share the value of the static variable (field, in Java terms) among all instances of a class.
An example of usage:
You can see that the value of instanceCounter is the same for all instances of Foo.
Static classes are quite different beasts, I won't enter in differences between inner and nested classes here.
Not necessarily: In Processing, and consequently in Java too, primitive + String fields
can be
static
as long it's alsofinal
, even in inner classes!I myself use that all the time to have constants inside classes! \m/
Mostly
static
makes possible for us to directly access fields & methods w/o instantiating them 1st! :bzBesides having shared field variables too.
Yes, I was talking about the real variable static fields, not about the constants, but I should have been clearer.
As said, static final fields in classes are less useful in Processing, but at least, they allow to scope them, and to avoid to pollute the global namespace!
Scope them: you can have a Button.WIDTH and a TextBox.WIDTH, for example, with different values.