Static modifier

Gosu supports static variables, methods, properties, and inner types.

Static variables

Gosu classes can define a variable as static. A static variable is stored once for a Gosu class, rather than once for each instance of the class. The static modifier can be used with variables and properties.

For example, you can create a static variable called _name using this syntax:

private static var _name : String

This example creates a static method called printedName:

public static function printedName() : String {
  return _name
}

You can initialize static variables either with a value or the result of a method call:

// Initialize _name with a value
private static var _name : String = "John Doe" 

// Initialize _copyOfName with result of static method call
private static var _copyOfName : String = printedName()
Warning: If you use static variables in a multi-threaded environment, you must take precautions to prevent simultaneous access from different threads. Use static variables sparingly if ever. If you use static variables, be sure you understand synchronized thread access fully.

For additional help on static variables and synchronized access, contact Customer Support.

To use a Gosu class variable, set its access level, such as internal or public, to restrict access to the set of classes that need to use the variable.

The static modifier cannot be combined with the abstract modifier.

Static constants

Gosu supports static constants within a class declaration, as shown in this example:
static final var DEFAULT_FIRST_NAME = "Default first name"
public static final var DEFAULT_LAST_NAME : String = "Default last name"

Static methods and properties

The static modifier can be used with methods and properties to indicate that the method or property belongs to the type itself rather than instances of the type.

The following example defines a static property and method:

class Greeting {

  private static var _name : String

  static property get Name() : String { 
    return _name 
  } 

  static property set Name(str : String) { 
    _name = str
  } 

  static function PrintGreeting() {
    print("Hello World")
  }
}

The Name property get and set functions and the PrintGreeting method are part of the Greeting class itself because they are marked as static.

The following code accesses properties on the class itself, not an instance of the class. You can create the class, and then test this code in the Gosu Scratchpad:

Greeting.Name = "Initial value"
print(Greeting.Name)
Greeting.PrintGreeting()

This example does not use the new keyword to construct an instance of the Greeting class.

Static inner types

The static modifier can also be used with inner types, such as inner classes. The static modifier on the inner type indicates that the inner type belongs to the outer type itself rather than instances of the type.

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.

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

package example

class Greeting {
  static class FrenchGreeting {
    static public function sayWhat() : String {
      return "Bonjour"
    }
  }

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

Create the classes, and then try the following lines in Gosu Scratchpad to test the inner class.

print(example.Greeting.Hello)

This code prints:

Bonjour

Static blocks and initialization

Gosu does not support static blocks. Guidewire recommends that you avoid static initialization and use lazy or on-demand initialization. If you need static blocks, use the following pattern:

class MyClass {
  static var _classInit = classInit()
  private static function classInit() : boolean {
    ...
  }
}
This pattern can also be expressed with the following notation:
class MyClass {
  static var _classInit = (-> {
    ...
  })()
}

See also