Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Java 7 diamond operator be used with anonymous classes?

Consider this Java code which attempts to instantiate some Lists:

List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList<>(); List<String> list3 = new ArrayList<String>() { }; List<String> list4 = new ArrayList<>() { }; List<String> list5 = new ArrayList<Integer>() { }; 

list1 and list2 are straightforward; list2 uses the new diamond operator in Java 7 to reduce unnecessary repetition of the type parameters.

list3 is a variation on list1 using an anonymous class, potentially to override some methods of ArrayList.

list4 attempts to use the diamond operator, similar to list2, but this is a compile error, with the message '<>' cannot be used with anonymous classes.

list5 produces an error that proves the compiler knows what type is actually needed. The error message is Type mismatch: cannot convert from new ArrayList<Integer>(){} to List<String>

So, with the declaration of list4, why can't the diamond operator be used with anonymous classes? There is a similar question here with an accepted answer that contains the following explanation from JSR-334:

Using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change.

I need some help understanding that reasoning. Why would an explicit type versus the identical and apparently easily inferred type require any difference in the resulting class file? What difficult use case would be covered by "doing so in general"?

What is the underlying reason for this?

like image 272
Dave Hartnoll Avatar asked Mar 05 '14 14:03

Dave Hartnoll


People also ask

What is the use of diamond operator in Java?

Diamond Operator: Diamond operator was introduced in Java 7 as a new feature. The main purpose of the diamond operator is to simplify the use of generics when creating an object. It avoids unchecked warnings in a program and makes the program more readable.

Are anonymous objects possible in Java?

Yes, we can use a method on an object without assigning it to any reference.

Can anonymous classes have constructors Java?

Since they have no name, we can't extend them. For the same reason, anonymous classes cannot have explicitly declared constructors.


1 Answers

This was discussed on the "Project Coin" mailing list. In substance (emphasis mine):

Internally, a Java compiler operates over a richer set of types than those that can be written down explicitly in a Java program. The compiler-internal types which cannot be written in a Java program are called non-denotable types. Non-denotable types can occur as the result of the inference used by diamond. Therefore, using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change. It is feasible that future platform versions could allow use of diamond when creating an anonymous inner class as long as the inferred type was denotable.

Note that it is not supported in Java 8 either but will be included as a new feature in Java 9 (Item 3 of "Milling Project Coin").

like image 137
assylias Avatar answered Sep 19 '22 15:09

assylias