Static inner classes

A static inner class is an inner class that is a singleton, which is a predefined single instance within its parent class. You do not instantiate this class.

The following example defines a static inner class called FrenchGreeting within the Greeting class:

package example

class Greeting {
  public property get Hello() : String {
    return FrenchGreeting.sayWhat()
  }

  // STATIC INNER CLASS DECLARATION
  static class FrenchGreeting {
    static public function sayWhat() : String {
      return "Bonjour"
    }
  }
}

You can test this class in the Gosu Scratchpad using the code:

print(example.Greeting.Hello)

This code prints:

Bonjour

Notice that this example does not construct a new instance of the Greeting class or the FrenchGreeting class using the new keyword. The inner class in this example has the static modifier.

An inner class must be declared static in order to define static members, such as static methods, static fields, or static properties. This Gosu requirement mirrors the requirement of Java inner classes.

See also