An object is a value that has the following features:
class - each object is an instance of a specific class, making it part of the class hierarchy, which is an important aspect of the type system;
properties - various fields and methods, depending on the class;
identity - it is distinct from all other objects, even if all the properties are the same.
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 create a new
JButton object (using
default constructor), and sets the
properties (by calling
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
but that only handle 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
you can call it with argumens
v2 using Colon notation:
(button1:paintImmediately 10 10 30 20)
If you prefer, you can use the
normally with a quoted method name:
(invoke button1 'paintImmediately 10 10 30 20)
You need to use
invoke (rather than colon notation)
obj is a
Class or a type expression, or its class
See Method operations for details.
obj has a field or property named
fld you can also use colon
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:
is equivalent to:
You can also change a field or property using colon notation:
(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
See Field operations for details.
Kawa views static properties and methods as properties and methods of the class itself. To call a static method use the syntax:
(java.math.BigDecimal:valueOf 12345 2) ⇒ 123.45
To access a static field do
. For example:
You can also use the