Importing types and package namespaces

To use types and namespaces in Gosu scripts without fully qualifying the full class name including the package, use a Gosu uses statement. The uses statement behaves similarly to the Java language’s import statement. To indicate that you are importing static members of a class, you use a # character and the members to import after the type name.

Always put uses statements at the beginning of the file or script. Do not add these lines within a Gosu method declaration.

Syntax

After the uses statement, specify a package namespace or a specific type such as a fully qualified class name.

uses type
uses namespace
uses type#static member
uses type#all static members

Namespaces can be specified with an asterisk (*) character to indicate a hierarchy. For example:

uses toplevelpackage.subpackage.*

Example 1

The following code uses a fully qualified type name.

var myInputStream = new java.io.FileInputStream()

Instead, you can use the following code that declares an explicit type with the uses operator.

// This "uses" expression provides access to the java.io.FileInputStream class
uses java.io.FileInputStream

// Use this simpler expression without specifying the full package name:
var myInputStream = new FileInputStream()

Example 2

The following code uses a fully qualified type name.

var myInputStream = new java.io.FileInputStream()

Instead, you can use the following code that declares a package hierarchy with the uses operator.

// This "uses" expression provides access to all the classes in the java.io package hierarchy
uses java.io.*

// Use this simpler expression without specifying the full package name:
var myInputStream = new FileInputStream()

Example 3

The following code uses a qualified type name. Because classes in the java.lang package are always in scope, you do not need to specify java.lang.

var circleRadius = 2.0
var circleCircumference = circleRadius * circleRadius * Math.PI
Instead, you can use the following code that declares a single static field from the java.lang.Math class.
// This "uses" expression provides access to the PI constant in the java.lang.Math class
uses java.lang.Math#PI

var circleRadius = 2.0
// Use this simpler expression without specifying the class name:
var circleCircumference = circleRadius * circleRadius * PI

Example 4

The following code uses a qualified type name. Because classes in the java.lang package are always in scope, you do not need to specify java.lang.
var mySquareArea = 1000
var mySideLength = Math.sqrt(mySquareArea)
You can use any of the angle constants in your code.
// This "uses" expression provides access to all the static members in the java.lang.Math class
uses java.lang.Math#*

var mySquareArea = 1000
// Use this simpler expression without specifying the class name:
var mySideLength = sqrt(mySquareArea)