Am i thinking wrongly about something: Clojure imports upon starting REPL all the Classes in the java.lang package. So one should be able to call methods from these classes without importing them. Now i am trying to play a bit with different classes of java.lang package, and i cant call some public methods in the form (.Method), for example (.totalMemory) of the Runtime Class throws IllegalArgumentException. Any ideas?
1 Answer
You need to use this:
(.totalMemory (Runtime/getRuntime))
Clojure does not automatically import all methods from all classes in java.lang. Also, the method totalMemory in class Runtime is not a static method, so you need to specify the instance to call the method on.
You get the instance by calling the static method getRuntime from class Runtime.
See Clojure - Java Interop for information about how to call a Java method from Clojure.
5 Comments
Jesper
Non-static methods must be called on an instance of a class (an object). Static methods can be called on the class itself and don't need to be called on an object.
amalloy
@AmirTeymuri That's a new question, not a comment on this one.
Jesper
@AmirTeymuri Have a look at Oracle's Java tutorials: Object-Oriented Programming Concepts. The Java API documentation lists the classes and objects available in the standard Java library.
(.totalMemory (Runtime/getRuntime))