Node:Field operations, Next:, Previous:Method operations, Up:Objects Classes and Modules



Accessing fields of Java objects

Kawa has both a high-level interface and a low-level interface for accessing the fields of Java objects and static fields. The lower-level interfaces are macros that return functions. These functions can be inlined, producing efficient code. The higher-level functions are less verbose and more convenient. However, they can only access public fields.

field object fieldname Function
Get the instance field with the given fieldname from the given Object. Returns the value of the field, which must be public. This procedure has a setter, and so can be used as the first operand to set!.

The field name is "mangled" (see Mangling) into a valid Java name. If there is no accessible field whose name is "fieldname", we look for a no-argument method whose name is "getFieldname".

If object is a primitive Java array, then fieldname can only be 'length, and the result is the number of elements of the array.

static-field class fieldname Function
Get the static field with the given fieldname from the given class. Returns the value of the field, which must be public. This procedure has a setter, and so can be used as the first operand to set!.

Examples:

(static-field <java.lang.System> 'err)
;; Copy the car field of b into a.
(set! (field a 'car) (field b 'car))

slot-ref object fieldname Function
A synonym for (field object fieldname).

slot-set! object fieldname value Function
A synonym for (set! (field object fieldname) value).