What is Double Brace initialization in Java?

前端 未结 13 2265
旧时难觅i
旧时难觅i 2020-11-21 07:22

What is Double Brace initialization syntax ({{ ... }}) in Java?

13条回答
  •  死守一世寂寞
    2020-11-21 07:47

    The first brace creates a new Anonymous Class and the second set of brace creates an instance initializers like the static block.

    Like others have pointed, it's not safe to use.

    However, you can always use this alternative for initializing collections.

    • Java 8
    List list = new ArrayList<>(Arrays.asList("A", "B", "C"));
    
    • Java 9
    List list = List.of("A", "B", "C");
    

提交回复
热议问题