Double brace initialisation (anonymous inner class) with diamond operator

后端 未结 4 1539
轻奢々
轻奢々 2020-12-10 13:44

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

error: cannot

4条回答
  •  悲哀的现实
    2020-12-10 14:09

    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 map1 = new HashMap() { //compiles fine
        {
            put("abc", "abc");
        }
    };
    

提交回复
热议问题