Next: Types and declarations, Previous: Lists and sequences, Up: Kawa Scheme Tutorial [Contents][Index]
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.
To create a new object of class T
you call T
as if it
were a function, passing it the various constructor arguments:
(java.io.File "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 creates 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 handles simple constructor calls:
(make java.io.File "src" "build.xml")
See Allocating objects for details.
Given an object obj of a class that has a method meth, you can call it with argumens v1 ... v2 using Property access 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 Calling Java methods from Scheme for details.
If obj has a field or property named fld you can also use colon notation:
obj:fld
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:
button1:tool-tip-text
is equivalent to:
(button1:getToolTipText)
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 Accessing object fields for details.
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:
java.awt.Color:RED
You can also use the static-field
and invoke-static
procedures.
Next: Types and declarations, Previous: Lists and sequences, Up: Kawa Scheme Tutorial [Contents][Index]