How can I call a java static method in clojure?

倾然丶 夕夏残阳落幕 提交于 2020-01-09 09:58:52

问题


I wish to call class on the String class. How can I access this static method?


回答1:


You can call a static method using (ClassName/methodName arguments).

However class is not a static method, it's a java keyword and you don't need it in clojure. To get the Class object associated with the String class, just use String.




回答2:


An example is worth 100 words:

(. String (valueOf 1))



回答3:


Class doesn't have a "class" method, nor a "class" member. The symbol String is mapped to the class java.lang.String, if what you want to get is the equivalent of Java's String.class. If you want to call a static method of the String class, the syntax is (String/methodName arg1 arg2).




回答4:


Your question has been answered, I think, but if you really do want to get the class of an unknown object, you can use the class function:

> (class "Foo")
java.lang.String

As in java, to specify classes outside of java.lang as literals, you need to either import them, or specify the full package + class name using dot (.) separators.



来源:https://stackoverflow.com/questions/5424520/how-can-i-call-a-java-static-method-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!