Calling Methods

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)))

Here we’ve created a new file object f and we’ve 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)

We can also chain multiple method calls together using the double ...

Get Web Development with Clojure, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.