Some Practical Clojure Examples
← prev | next →     Top-level ToC     /java-interop.html     (printable version)

Recall:

  • Java has classes.
  • Static (class) and instance variables are called “data members” (or “fields”).
  • Class names go like SomeClass.
  • Classes may have instance (per-object) data members, and methods (named likeThis).
  • Classes may have static (per-class) data members, and methods (also named likeThis).
  • Constant static class fields are named LIKE_THIS.
;; Accessing a static data member:
(. Math PI)  ; sugarless
Math/PI      ; customary
;; `Math.PI` ; Java

;; Calling a static method:
(. Math sin 0.5)   ; sugarless
(Math/sin 0.5)     ; customary
;; `Math.sin(0.5)` ; Java

;; Creating a new instance:
(new SomeClass arg1 arg2)      ; sugarless
(SomeClass. arg1 arg2)         ; customary
;; `new SomeClass(arg1, arg2)` ; Java

;; Accessing an instance data member:
;; (Use the dash to ensure data member access only,
;; and not zero-arg instance method call.)
(. someInstance -someMember)  ; sugarless
(.-someMember someInstance)   ; customary
;; `someInstance.someMember`  ; Java

;; Calling an instance method:
(. someInstance someMethod arg1 arg2)    ; sugarless
(.someMethod someInstance arg1 arg2)     ; customary
;; `someInstance.someMethod(arg1, arg2)` ; Java