class Outer {
static void out() {
System.out.println("hello world");
}
static class Inner {
}
static void main(String[] args) {
//Intellij Note: No candidates found for method call
Outer.Inner.out();
}
}
After running this script, the Outer.out will be executed successuful. Why?
I get this info from the official doc:
The implementation of anonymous inner classes and nested classes follow Java closely, but there are some differences, e.g. local variables accessed from within such classes don’t have to be final. We piggyback on some implementation details we use for groovy.lang.Closure when generating inner class bytecode.
Anybody can explain why this script can be executed successful?
out()is accessible for inner class - this is correct for java:static class Inner { static void inn(){ out(); } }. so, in groovy when you have accessorOuter.Inner.you switching context to Inner class whereout()is accessible. maybe it's a bug, but for me it sounds logical ;) you can always annotate your class or method with@groovy.transform.CompieStaticto get compilation error as in java.