10

I can use (.toUpperCase "GOOD") in clojure, as "GOOD" is java string, and java string has toUpperCase method.

I also can use (java.io.File/separator) from clojure as a way of calling java functions.

But, why can't I call (java.lang/Object wait 3) or (java.lang.System/println "hi")?

  • Can't we use all the java functions from Clojure?
  • If not, is there any rule for calling them? If so, where are the reference for those functions?
asked Aug 4, 2010 at 14:51

3 Answers 3

18

You can use all Java functions from Clojure. See the great page on Clojure's Java interop.

In particular, you just need to get the syntax right depending on exectly what sort of Java construct you are dealing with, e.g. executing the println method on the static member "out" from java.lang.System:

(.println (System/out) "hi")
answered Aug 4, 2010 at 14:57
Sign up to request clarification or add additional context in comments.

1 Comment

No probs, the reason is that separator is a static member of the File class, in the same way that out is a static member of the System class. In the println case, you are actually chaining together two Java interop constructs - accessing System.out, and then calling println on the result.
7

As the earlier poster noted, the two examples you give are just a little off:

(.wait (java.lang.Object.) 3) ;; this actually throws an IllegalMonitorStateException
(.println java.lang.System/out "hi")

Should work!

answered Aug 4, 2010 at 15:45

Comments

3

At first, Object.wait() function is not a static function, you should use as:

(.wait (java.lang.Object.) 3)

Second, Object.wait() function should be called after you get the lock. Otherwise, it will throw IllegalMonitorStateException.

answered Jan 17, 2014 at 7:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.