When you compile a Java class with a private inner class, it seems that the anonymous class is automatically synthesized with it for some reason. This class is enough to reproduce it:
public class SynthesizeAnonymous {
public static void method() {
new InnerClass();
}
private static class InnerClass {}
}
When compiled, this generates the expected files SynthesizeAnonymous.classand SynthesizeAnonymous$InnerClass.class, but also generates a strange SynthesizeAnonymous$1.classfile that corresponds to the anonymous subclass java.lang.Objectthat was synthesized. If you look at disassembly with javap, then by default the constructor InnerClassgets a hidden parameter of this anonymous type and what nullis passed to it when called new InnerClass().
Why is this class created? It is created even if InnerClassit is not static, but it is not created if InnerClassit is not private or the instance is InnerClassnever created. Is this some form of access control? How it works?
source
share