java.util and anonymous class

Rémi Forax forax at
Mon Dec 5 10:20:11 UTC 2011

While removing generics warnings in java.util,
I've seen that some anonymous classes are coded in a way that
make javac to generate too much fields.

This code:
final List<String> list = Arrays.asList(args);
     Iterator<String> iterator = new Iterator<String>() {
       private Iterator<String> it = list.iterator();


generates an anonymous class with two fields:
final class Foo$1 implements java.util.Iterator<java.lang.String> {
   java.util.Iterator<java.lang.String> it;
   final java.util.List val$list;

while this code:
List<String> list = Arrays.asList(args);
     final Iterator<String> it = list.iterator();
     Iterator<String> iterator = new Iterator<String>() {

generates an anonymous class with only one field:
final class FinalCaptureTooBig$1 implements 
java.util.Iterator<java.lang.String> {
   final java.util.Iterator val$it;

If javac was smart, it should generate only one field because the 
variable 'list' is only
use in the initializer of a field, so 'list' has to be passed as 
parameter of the constructor
but don't need to be stored as a field.

Note that this change in javac may break the serialization compatibility,
so may be it's not a good idea to change javac. Or we can say that we 
don't care
because there is an item in Effective Java that says that you should not use
an anonynous class.

Should we change the code of java.util or javac ?


More information about the core-libs-dev mailing list