January 2014
Intermediate to advanced
232 pages
5h 11m
English
Once we have an instance of a class, we can call methods on it. The notation is similar to making a regular function call. When we call a method, we pass the object its first parameter followed by any other parameters that the method accepts.
| | (let [f (File. ".")] |
| | (println (.getAbsolutePath f))) |
There, we created a new file object f, and we called .getAbsolutePath on it. Notice that methods have a period (.) in front of them to differentiate them from regular Clojure functions. If we wanted to call a static function or a variable in a class, we would use the / notation, as follows.
| | (str File/separator "foo" File/separator "bar") |
| | |
| | (Math/sqrt 256) |
There’s also a shorthand for chaining multiple method calls ...
Read now
Unlock full access