37

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

asked Mar 24, 2011 at 19:34

4 Answers 4

55

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.

answered Mar 24, 2011 at 19:40
Sign up to request clarification or add additional context in comments.

Comments

26

An example is worth 100 words:

(. String (valueOf 1))
om-nom-nom
63k13 gold badges186 silver badges231 bronze badges
answered Mar 24, 2011 at 19:38

1 Comment

If I have an instance of a class but not the class name itself, how can I call a static method? (. (class my-instance) staticMethod 10). I get an error "No matching method staticMethod found taking 1 args for class java.lang.Class", even though (class my-instance) does not return java.lang.Class.
7

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.

answered Mar 24, 2011 at 21:47

Comments

6

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).

answered Mar 24, 2011 at 19:40

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.