Why is an anonymous inner class containing nothing generated from this code?

I’m using polygenelubricants’s smaller snippet. Remember there’s no concept of nested classes in the bytecode; the bytecode is, however, aware of access modifiers. The problem the compiler is trying to circumvent here is that the method instantiate() needs to create a new instance of PrivateInnerClass. However, OuterClass does not have access to PrivateInnerClass‘s constructor (OuterClass$PrivateInnerClass … Read more

What is the $1 in class file names?

Those are the .class files that hold the anonymous inner classes. In your example WelcomeApplet.java contains a top-level class (called WelcomeApplet) and an anonymous inner class, which will be stored in WelcomeApplet$1.class. Note that the exact name of the files holding anonymous inner classes is not standardized and might vary. But in practice I’ve yet … Read more

Java 8 Lambda Expressions – what about multiple methods in nested class

From JLS 9.8 A functional interface is an interface that has just one abstract method, and thus represents a single function contract. Lambdas require these functional interfaces so are restricted to their single method. Anonymous interfaces still need to be used for implementing multi-method interfaces. addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { … … Read more

How are Anonymous inner classes used in Java?

By an “anonymous class”, I take it you mean anonymous inner class. An anonymous inner class can come useful when making an instance of an object with certain “extras” such as overriding methods, without having to actually subclass a class. I tend to use it as a shortcut for attaching an event listener: button.addActionListener(new ActionListener() … Read more