Name

static

Description

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 variable. When a class is defined with the static keyword, it's methods can be used without making an instance of the class. The above examples demonstrate each of these uses.

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.

Examples

  • void setup() {
      MiniClass mc1 = new MiniClass();
      MiniClass mc2 = new MiniClass();
      println( mc1.y );   // Prints "10" to the console
      MiniClass.y += 10;  // The 'y' variable is shared by 'mc1' and 'mc2'
      println( mc1.y );   // Prints "20" to the console
      println( mc2.y );   // Prints "20" to the console
    }
    
    static class MiniClass {
      static int y = 10;  // Class variable
    }
    
  • void setup() {
      println(MiniClass.add(3, 4));  // Prints "7" to the console
    }
    
    static class MiniClass {
      static int add(int x, int y) {
        return(x + y);
      } 
    }