What is Double Brace initialization in Java?

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

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

相关标签:
13条回答
  • 2020-11-21 07:43

    Double brace initialisation creates an anonymous class derived from the specified class (the outer braces), and provides an initialiser block within that class (the inner braces). e.g.

    new ArrayList<Integer>() {{
       add(1);
       add(2);
    }};
    

    Note that an effect of using this double brace initialisation is that you're creating anonymous inner classes. The created class has an implicit this pointer to the surrounding outer class. Whilst not normally a problem, it can cause grief in some circumstances e.g. when serialising or garbage collecting, and it's worth being aware of this.

    0 讨论(0)
  • You can put some Java statements as loop to initialize collection:

    List<Character> characters = new ArrayList<Character>() {
        {
            for (char c = 'A'; c <= 'E'; c++) add(c);
        }
    };
    

    Random rnd = new Random();
    
    List<Integer> integers = new ArrayList<Integer>() {
        {
             while (size() < 10) add(rnd.nextInt(1_000_000));
        }
    };
    

    But this case affect to performance, check this discussion

    0 讨论(0)
  • 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<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
    
    • Java 9
    List<String> list = List.of("A", "B", "C");
    
    0 讨论(0)
  • 2020-11-21 07:48

    I think it's important to stress that there is no such thing as "Double Brace initialization" in Java. Oracle web-site doesn't have this term. In this example there are two features used together: anonymous class and initializer block. Seems like the old initializer block has been forgotten by developers and cause some confusion in this topic. Citation from Oracle docs:

    Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

    {
        // whatever code is needed for initialization goes here
    }
    
    0 讨论(0)
  • 2020-11-21 07:51

    For a fun application of double brace initialization, see here Dwemthy’s Array in Java.

    An excerpt

    private static class IndustrialRaverMonkey
      extends Creature.Base {{
        life = 46;
        strength = 35;
        charisma = 91;
        weapon = 2;
      }}
    
    private static class DwarvenAngel
      extends Creature.Base {{
        life = 540;
        strength = 6;
        charisma = 144;
        weapon = 50;
      }}
    

    And now, be prepared for the BattleOfGrottoOfSausageSmells and … chunky bacon!

    0 讨论(0)
  • 2020-11-21 07:52

    This would appear to be the same as the with keyword so popular in flash and vbscript. It's a method of changing what this is and nothing more.

    0 讨论(0)
提交回复
热议问题