In Clojure how can I read a public member variables of an instance of a Java class? I want something like:
(. instance publicMemberName)
I also tried:
instance/publicMemberName
but this only works with static methods
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Java, the class
java.awt.Pointhas public fieldsxandy. See the javadocs here http://download.oracle.com/javase/6/docs/api/java/awt/Point.html.In Clojure the dot macro works for fields and methods. This worked for me:
EDIT: The following also works (note the space between the dot and the p):
EDIT: I decided to make a complete example given that
java.awt.Pointhas methodsgetXandgetYin addition to public fieldsxandy. So here goes. First make a Java class like this:Compile it
Now move
C.classinto your clojure directory. Next start the REPL, import the class, and watch it work:Note the other way works too: