I would so much rather like to write this:
Lists.transform(vals,
new Function<>() {
public List<ValEntry> apply(Validator<? super T> input) {
return input.validate(value);
}
});
...than this:
Lists.transform(vals,
new Function<Validator<? super T>, List<ValEntry>>() {
public List<ValEntry> apply(Validator<? super T> input) {
return input.validate( value );
}
});
But the Java compiler gives me the following error message:
'<>' cannot be used with anonymous classes
Is there a fundamental reason for this? Or did the just skip the feature in JDK 7, maybe they do it in 8?
Because the EventHandler<ActionEvent> interface contains only one method, you can use a lambda expression instead of an anonymous class expression. See the section Lambda Expressions for more information. Anonymous classes are ideal for implementing an interface that contains two or more methods.
Java anonymous inner class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as overloading methods of a class or interface, without having to actually subclass a class.
It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.
3.1. Since they have no name, we can't extend them. For the same reason, anonymous classes cannot have explicitly declared constructors.
According to the project coin documentation:
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.
EDIT So it's possible in a future version. It's still not possible with Java 8, but now we have lambdas, so there's less of a need.
This is now scheduled to be included in Java 9. From JEP 213: Milling Project Coin:
- Allow diamond with anonymous classes if the argument type of the inferred type is denotable. Because the inferred type using diamond with an anonymous class constructor could be outside of the set of types supported by the signature attribute, using diamond with anonymous classes was disallowed in Java SE 7. As noted in the JSR 334 proposed final draft, it would be possible to ease this restriction if the inferred type was denotable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With