package org.tomko.konkordans;
public class NestedStatics {
public static String FOO = "foo";
public static class LevelOne {
public static enum LevelTwo {
YES,NO;
}
}
}
Then, at the REPL:
user=> (str org.tomko.konkordans.NestedStatics/FOO)
"foo"
user=> (str.org.tomko.konkordans.NestedStatics/LevelOne)
java.lang.ClassNotFoundException:
str.org.tomko.konkordans.NestedStatics (NO_SOURCE_FILE:0)
user=>
So, it's clear that I have the NestedStatics class in my classpath,
and that I can reference static members that are of a fundamental data
type. But attempting to reach in one level deeper does not succeed.
I also tried importing LevelOne into my namespace using a :use, but
that didn't work - although I'm a bit hazy on the precise syntax for
that so it might be my fault. A search in this forum didn't turn up
anything quite like this, but there was some discussion about import
statics that might be relevant.
>user=> (str.org.tomko.konkordans.NestedStatics/LevelOne)
Does
str.org.tomko.konkordans.NestedStatics$LevelOne/ONE
Work?
You can always look in the class output directory that the Java
compiler generates, and see the resulting class name that it creates
(just change the slashes in the path into dots).
David
user=> (str org.tomko.konkordans.NestedStatics$LevelOne)
"class org.tomko.konkordans.NestedStatics$LevelOne"
Is this depending on a detail of implementation? It feels funny to
me, but perhaps it's the best way to do it.
(ns org.tomko.konkordans.analysis
(:import
(org.tomko.konkordans NestedStatics)))
(def foo NestedStatics$LevelOne$LevelTwo/NO)
user=> (load-file "/Users/mark/IdeaProjects/Konkordans/src/org/tomko/
konkordans/analysis.clj")
java.lang.Exception: No such namespace: NestedStatics$LevelOne
$LevelTwo (analysis.clj:5)
The class is called NestedStatics$LevelOne$LevelTwo, so you would have
to import that if you wanted to use it. As far as the JVM is
concerned, NestedStatics, NestedStatics$LevelOne and
NestedStatics$LevelOne$LevelTwo are just three independent classes.
Java added nested classes, but didn't really add them to the JVM. It
just makes longer class names using the '$'.
It's probably safe to rely on this behavior, since there is plenty of
code that depends upon it working this way.
David