Java Double Brace Initialization

Last modified on August 1st, 2014 by Joe.

Double brace initialization is a combination of two separate process in java. There are two { braces involved in it. If you see two consecutive curly braces { in java code, it is an usage of double brace initialization.

Java Double Brace Initialization

First brace is creation of an anonymous inner class. Without considering the second brace if you see the first brace alone then its nothing new for us. We have created many anonymous inner classes in such a way.

Second brace is an initialization block. That too you have seen in it a class for initialization. You may have used a static initialization block. When you use the initialization block for an anonymous inner class it becomes java double brace initialization. The inner class created will have a reference to the enclosing outer class. That reference can be used using the ‘this’ pointer.

Example for double brace initialization

class JavaPapers {
....
....
add(new JPanel() {{
setLayout(...);
setBorder(...);
...
}}
);
}

Uses of double brace initialization

Using double brace initialization, we can initialize collections. Though people say, it is easier to initialize a constant collection using double brace initialization. I don’t see any advantage in using double brace initialization to initialize collections. In jdk 1.7 there is going to be an exclusive construct for that. Then java double brace initialization will become completely obsolete.

...
myMethod(
  new ArrayList() {{
     add("java");
     add("jsp");
     add("servlets");
  }}
);
...

Note on double brace initialization

Serialization of non-static inner class has an issue. The outer class will also get serialized. Because the inner class has got a reference to the outer class and it is implicit. Since it is implicit, it cannot be marked as transient. This issue is also applicable for double brace initialization.

Comments on "Java Double Brace Initialization"

  1. Swetha says:

    Till now I don’t know this double brace initialization!

    Man you are amazing… thanks thanks..

  2. Eric Jablow says:

    This is useful, but try to avoid it with classes where equals and hashCode are important. A class whose equals(Object o) method checks that this.getClass() == o.getClass() will fail badly with instance initializers.

    X x0 = new X() {{ a = 3; }};

    X x1 = new X() {{ a = 3; }};

    assert x0.equals(x1); // fails

  3. Sergey says:

    What about Java’s PermGen space?

  4. Sergey says:

    What’s about PermGen memory space? Every time you’re using double brace initialization you’re creating a new subclass which has to be loaded.

  5. Rahul says:

    it’s amazing yar…
    double {{ intialization ……

  6. Adam Malter says:

    This is a neat and useful trick.. But is really anti-pattern when you look closer. (Following the pattern of least surprise)

    You make a new anonymous class each time you do this – causing surprise around this.getClass(). As a previous commenter pointed out, this causes havoc with .equals() .hashCode() and even logging. Additionally, you clog up your classloader (and permgen)

    tl dr;A neat syntatic trick but harmful.

  7. Hariprasath says:

    It is really useful to handle&crack the interviews…I’m very inspired that blog..

  8. Suyog says:

    private static final Set VALID_CODES = new HashSet();
    static {
    validCodes.add(“XZ13s”);
    validCodes.add(“AB21/X”);
    validCodes.add(“YYLEX”);
    validCodes.add(“AR2D”);
    }

    ——
    using double brace

    private static final Set VALID_CODES = new HashSet() {{
    add(“XZ13s”);
    add(“AB21/X”);
    add(“YYLEX”);
    add(“AR2D”);
    }};

  9. vidhi says:

    I like it, it was new thing for me. Thanks

  10. Dinesh says:

    Good

  11. samir says:

    very nice article.

  12. Chandraprakash Sarathe says:

    Nice Stuff.

  13. Daniele says:

    Nice article. Thanks!

  14. Jigar says:

    Nice Article and very useful to improve..
    technical knoweldge of java

  15. inderjot says:

    its always a pleasure to read this blog.

  16. Anonymous says:

    good point known to me.

    Thanks

  17. Shazz says:

    1st brace creates Anonymous Inner class

    2nd brace is called instance initializer, because it is declared within the instance scope of the class.

    Initialization block is invoked before constructor is called.

    And this works only for non final class because it needs to create an anonymous class which we cannot do in final classes.

    And there are 2 prominent issues with double brace initialization.
    1. creates problem for classes which uses equals and hashCode.
    2. Serialization of this class is a problem as it is non static.

    Thanks

  18. payal says:

    really good stuff on java.
    i m reading this blog for the first time…

  19. notard says:

    comment avoir la version en Français svp.
    merci

    j’attend votre réponse avec impatience

  20. Vijay S says:

    Knowing that something like this exist in java was great

  21. raghu says:

    Sir ,

    this double brace initialization seems to be like with() in .net.isn’t so?

    or am i miss-understood?

  22. vmbharathi says:

    Perfect! this helped me a lot to debug my serialization issue!!
    Thanks

  23. Raghu says:

    Hi,

    How to send and receive sms from/to java web application.?What package helps us to do so?

  24. […] a Hashtable in java is created using the empty constructor Hashtable(). Which is a poor decision and an often repeated mistake. Hashtable has two other […]

Comments are closed for "Java Double Brace Initialization".