Double brace initialisation (anonymous inner class) with diamond operator

纵饮孤独 提交于 2019-11-27 06:02:36

问题


I am wondering why the second map declaration (using the diamond operator) does not compile when the first one does. Compilation error:

error: cannot infer type arguments for HashMap; Map map2 = new HashMap<>() { reason: cannot use '<>' with anonymous inner classes where K,V are type-variables: K extends Object declared in class HashMap V extends Object declared in class HashMap

Code:

    Map<String, String> map1 = new HashMap<String, String>() { //compiles fine

        {
            put("abc", "abc");
        }
    };

    Map<String, String> map2 = new HashMap<>() { //does not compile

        {
            put("abc", "abc");
        }
    };

EDIT
Thanks for your answers - I should have read the compilation error better. I found the exaplanation in the JLS

It is a compile-time error if a class instance creation expression declares an anonymous class using the "<>" form for the class's type arguments.


回答1:


You don't have a static initializer here (the keyword static is missing altogether).

Basically you create a new anonymous subclass of HashMap and define the instance intializer block here. Btw, this only works since HashMap is not final.

Since you'll get an anonymous subclass of HashMap the diamond operator doesn't work here, since the subclass would then be compiled as if you wrote ... extends HashMap<Object, Object> and this clearly isn't compatible to Map<String, String>.




回答2:


Diamond inference doesn't work for instantiating anonymous classes, which is what you're doing here.

Try this:

Map<String, String> map1 = new HashMap<>();

{
    map1.put("abc", "abc");
}



回答3:


This feature is part of Project Coin 2 and will be avalaible in Java 9 coming September 22 of 2016.

It's call Allowing the diamond syntax with some anonymous class constructors.

Link.




回答4:


Note that you could also omit the diamond altogether. However, while this compiles, it's only because it's ignoring the Java generics and leans on the fact that Java is backwards compatible with previous versions.

Map<String, String> map1 = new HashMap() { //compiles fine
    {
        put("abc", "abc");
    }
};


来源:https://stackoverflow.com/questions/9773733/double-brace-initialisation-anonymous-inner-class-with-diamond-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!