Overview of access to Java types

Gosu provides full access to Java types. Gosu is built on top of the Java language and runs within the Java Virtual Machine (JVM). Gosu loads all Java types, so you have full direct access to Java types, such as classes, libraries, and primitive (non-object) types. You can use your favorite Java classes or libraries directly from Gosu with the same syntax as for native Gosu objects.

The most common Java object types retain their fully qualified name but are always in scope, so you do not need to fully qualify their names in typical code. Examples of classes that are always in scope are Object, String, and Boolean. If your code has ambiguity because of similarly named types also in scope, you must fully qualify these types, such as java.lang.String. Other Java types are available to Gosu code, but must be fully qualified, for example java.io.DataInputStream.

For example, for standard Gosu coding with lists of objects, use the Java type java.util.ArrayList. The following simple example uses a Java-like syntax:

var list = new ArrayList<String>()
list.add("Hello Java, from Gosu")

Gosu includes transformations on Java types that make your Gosu code clearer, such as turning getters and setters into Gosu properties.

Access to Java types from Gosu includes:

  • Instantiation of Java classes with the new keyword
  • Implementing Java interfaces
  • Manipulating Java objects as native Gosu objects
  • Calling object methods on instantiated objects
  • Exposing object methods that look like getters and setters as properties
  • Calling static methods on Java types
  • Providing built-in extensions and improvements of many common Java types by using Gosu enhancements
  • Support for your own extensions of Java types and interfaces
  • Support for Java primitive types

See also