Cannot call inherited static methods

80 views
Skip to first unread message

Stuart Sierra

unread,
Aug 8, 2008, 4:29:53 PM8/8/08
to Clojure
String is an instance of Class, but I can't call Class.getName on it:
user=> (.getName String)
java.lang.NoSuchFieldException: getName
clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:68:
getName

Similarly:
user=> (. String (getName))
java.lang.IllegalArgumentException: No matching method: getName
clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:71: No
matching method: getName

This works:
user=> (.getName (class (new String)))
"java.lang.String"

-Stuart Sierra

Chouser

unread,
Aug 8, 2008, 4:49:04 PM8/8/08
to clo...@googlegroups.com
On Fri, Aug 8, 2008 at 4:29 PM, Stuart Sierra
<the.stua...@gmail.com> wrote:
>
> String is an instance of Class, but I can't call Class.getName on it:
> user=> (.getName String)
> java.lang.NoSuchFieldException: getName
> clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:68:
> getName

It has to do with String being both a class and a object. Or
something. I dunno, but it's come up before and the magic recipe is
to wrap the classname with any kind of function. "identity" is handy:

user=> (.getName (identity String))
"java.lang.String"

--Chouser

lpetit

unread,
Aug 8, 2008, 5:56:06 PM8/8/08
to Clojure
Hello,
The java type String is by no way an instance of Class. In java, types
(classes, interfaces, enums) are not instances of some sort of
metatypes (as do smalltalk for example), they are just ... types !
(AFAIK)

In java, you can get an instance of Class describing a java type by
several ways :

1/ call getClass on an instance of the type
2/ write the type name suffixed by .class, such as String.class

So the solution for you is a slight variation of the solution you
gave :

(.getClass (class String)) does the job. (No need to instanciate a
String instance).

HTH,

--
Laurent

On Aug 8, 10:49 pm, Chouser <chou...@gmail.com> wrote:
> On Fri, Aug 8, 2008 at 4:29 PM, Stuart Sierra
>

Rich Hickey

unread,
Aug 8, 2008, 6:12:14 PM8/8/08
to Clojure
(. String (getName))

says - call String.getName() - fails, getName is not a static method
of class String

'.' is a special form with special evaluation rules. In particular, it
checks to see if the first arg (unevaluated) names a class, if so the
call is considered a static call of that class. getName is not a
static method of class String.

In normal evaluation contexts, String represents the class object
String, which is of type Class. You want to call an instance method on
this object. To do so, you need to bypass the special evaluation of
the '.' here - identity makes the most sense:

(. (identity String) (getName))

says String.class.getName() - works getName is an instance method of
class Class

Rich

lpetit

unread,
Aug 8, 2008, 6:19:08 PM8/8/08
to Clojure
Yup, read (.getName (class String)) instead of (.getClass (class
String)), of course.
BTW, Rich gave the most meaningful solution.

--
Laurent
Reply all
Reply to author
Forward
0 new messages