NotSerializableException on anonymous class

Joshua Bloch writes in his book Effective Java, 2nd Edition, Item 74: Inner classes should not implement Serializable. They use compiler-generated synthetic fields to store references to enclosing instances and to store values of local variables from enclosing scopes. How these fields correspond to the class definition is unspecified, as are the names of anonymous … Read more

How to pass parameters to anonymous class?

Yes, by adding an initializer method that returns ‘this’, and immediately calling that method: int myVariable = 1; myButton.addActionListener(new ActionListener() { private int anonVar; public void actionPerformed(ActionEvent e) { // How would one access myVariable here? // It’s now here: System.out.println(“Initialized with value: ” + anonVar); } private ActionListener init(int var){ anonVar = var; return … Read more