(ns training.core (:import (java.util Date)))
Sitting in my favorite coffeehouse with a new notebook and a hot cup of java is my idea of Heaven.
(ns training.core (:import (java.util Date)))
(Date.) (Date. 2018 02 17)
Which is equivalent to the less used variant:
(new Date) (new Date 2018 02 17)
(.length "hello world") (.isDirectory (java.io.File. "my-dir"))
Equivalent to the less used variant:
(. "hello world" length) (. (java.io.File. "my-dir") isDirectory)
Java static method calls:
(Math/pow 1 2) (.print System/out "hi")
Inner classes:
java.nio.channels.FileChannel$MapMode/READ_ONLY
(ns training.core (:import (java.util HashMap))) (doto (HashMap.) (.put "a" 1) (.put "b" 2)) => {"a" 1, "b" 2}
We get the constructed object, with side-effects applied
reify
creates an object that conforms to an interface:
(.listFiles (java.io.File. ".") (reify java.io.FileFilter (accept [this f] (.isDirectory f))))
Notice that we did not define a class?
gen-class
creates a class.
proxy
extends a concrete superclass.
Rarely needed, refer to manual.
You can define Java classes in Java in a separate directory and add
:java-source-paths ["java-src"]
To your project.clj
file
lein compile
Java code in that directory will be usable from Clojure