Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error: "'<>' cannot be used with anonymous classes" [duplicate]

Tags:

java

java-7

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?

like image 976
Lii Avatar asked Aug 27 '11 10:08

Lii


People also ask

Can anonymous class have multiple methods?

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.

What is the use of anonymous class in Java?

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.

What is an anonymous class in Java example?

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.

Can an anonymous class have a constructor Why How will you create object for it?

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


2 Answers

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.

like image 174
Craig P. Motlin Avatar answered Sep 21 '22 17:09

Craig P. Motlin


This is now scheduled to be included in Java 9. From JEP 213: Milling Project Coin:

  1. 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.
like image 4
Lii Avatar answered Sep 20 '22 17:09

Lii