Creating and using objects

An object is a value that has the following features:

We later discuss how to write a new class. Here we assume you’re using an existing class, which could be written in Java or Scheme.

Creating a new object

To create a new object of class T you call T as if it were a function, passing it the various constructor arguments:

( "src" "build.xml")

If there are keyword arguments they are used to initialize the corresponding named properties:

(! button1 (javax.swing.JButton text: "Do it!" tool-tip-text:  "do it"))

This create a new JButton object (using JButton’s default constructor), and sets the text and tool-tip-text properties (by calling JButton’s setText and setToolTipText methods). If there are constructor arguments, they must come before the keywords.

For objects that have components or elements, you can list these at the end. For example:

(java.util.ArrayList 11 22 33)

This creates a fresh java.util.ArrayList (using the default constructor), and then calls the add method 3 times.

If you prefer you can use the make procedure, but that only handle simple constructor calls:

(make "src" "build.xml")

See Allocating objects for details.

Calling instance methods

Given an object obj of a class that has a method meth, you can call it with argumens v1 ... v2 using Colon notation:

(obj:meth v1 ... v2)

For example:

(button1:paintImmediately 10 10 30 20)

If you prefer, you can use the invoke procedure, normally with a quoted method name:

(invoke button1 'paintImmediately 10 10 30 20)

You need to use invoke (rather than colon notation) if obj is a Class or a type expression, or its class implements gnu.mapping.HasNamedParts.

See Method operations for details.

Accessing properties

If obj has a field or property named fld you can also use colon notation:


You use the same syntax whether fld is an actual field in the object, or a property (in the Java Beans sense). The latter is implemented using a getter/setter pair: Methods named getF and setF, respectively. For example:


is equivalent to:


You can also change a field or property using colon notation:

(set! obj:fld value)

For example:

(set! button1:tool-tip-text "really do it!")

This is equivalent to:

(button1:setToolTipText "really do it!")

Instead of colon notation, you can use the field procedure.

See Field operations for details.

Static fields and methods

Kawa views static properties and methods as properties and methods of the class itself. To call a static method use the syntax:

(clas:meth v1 ... vn)

For example:

(java.math.BigDecimal:valueOf 12345 2) ⇒ 123.45

To access a static field do clas:fld. For example:


You can also use the static-field and invoke-static procedures.