How to instantiate an inner class

269 views
Skip to first unread message

Dustin Withers

unread,
Jun 24, 2008, 12:02:33 AM6/24/08
to Clojure
What is the correct syntax to instantiate an inner class?

I have some code in Java that looks like:

public abstract class DialPointer extends AbstractDialLayer
implements DialLayer, Cloneable, PublicCloneable, Serializable
{
// blah blah

public static class Pin extends DialPointer {
// more blah blah
}
}

I've tried:

(new DialPointer.Pin)
(new (. DialPointer Pin))
(new DialPointer$Pin) ;; Found that somewhere on the forums

Could someone enlighten me? If I hadn't been searching for the last 2
hours I wouldn't have posted again with more newbie problems.

Thanks,
-dustin

Stephen C. Gilardi

unread,
Jun 24, 2008, 12:16:03 AM6/24/08
to clo...@googlegroups.com
A nested class's full name is the name of the parent class followed by
$ followed by the nested class's name. The last example you gave
should have worked. Might you be missing an "import" expression?
Failing that, a fully qualified class name (including package) should
work.

(import '(dustins.cool.classes DialPointer$Dial))

(def p (new DialPointer$Pin))
(def q (DialPointer$Pin.))

(def r (new dustins.cool.classes.DialPointer$Dial))

--Steve

Dustin Withers

unread,
Jun 24, 2008, 12:19:00 AM6/24/08
to Clojure
Got it! That saved me. I wasn't importing DialPointer$Dial, I was only
importing DialPointer. You saved me again Steve. Thanks. BTW, where is
the documentation for this?

-dustin

Stephen C. Gilardi

unread,
Jun 24, 2008, 12:35:56 AM6/24/08
to clo...@googlegroups.com
> Got it! That saved me. I wasn't importing DialPointer$Dial, I was only
> importing DialPointer. You saved me again Steve. Thanks. BTW, where is
> the documentation for this?

Import is documented in the "namespaces" section of the website. It's
also on the API page (http://clojure.org/API#import). The docs don't
appear to mention the inner class naming issue specifically. The use
of "$" to designate an inner class is apparently a JVM thing, not a
Clojure thing. In Java, it's not exposed to the programmer while in
Clojure it is.

You're quite welcome. I'm glad you're back in action.

--Steve

Reply all
Reply to author
Forward
0 new messages