0

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?

2
  • What exactly are you typing in to the REPL? Commented Oct 3, 2015 at 12:28
  • 1
    Imports in java don't work like that. All instance methods need to be associated with a target object. Do something like (.totalMemory (Runtime/getRuntime)) Commented Oct 3, 2015 at 12:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

i have no experience in java, this means java static methods don't need an argument (like Math/PI) and the non-static ones should get an arg?
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.
Could you explain shortly what an object is, and how could i know which objects are available for certain methods(for example getRuntime for totalMemory)?
@AmirTeymuri That's a new question, not a comment on this one.
@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.

Your Answer

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