Skip to content
April 20, 2011 / oop123

Double Braces Initialization

As a programming beginner, I like to search for interesting programming stuff. Today I came across the Double Braces Initialization construct – a type of syntactic sugar that eases the construction of a Collection. Normally, to construct a Collection (I will use ArrayList as an example), you would do this:

ArrayList temp = new ArrayList();

It looks a little unsightly with the temporary variable, but with Double Braces Initialization, you can get away with this:

doSomethingWithAnArrayList(new ArrayList() {{

How does it work? Well, in a class definition, there a special structure called the (instance) initialization block. Essentially what the initialization block does is whenever the an instance of the class containing it is created, the code contained inside it will run. See the following code for a better explanation:

class Foo {
    //the following statement will execute whenever an instance of Foo is created
        this.x = 1; //this refers to the current instance of Foo
        x = 2;      //this is equivalent to the statement this.x = 2

The Double Braces Initialization cleverly uses the initialization block to do its magic. The code “new ArrayList()” from the example above creates an anonymous subclass of ArrayList. The initialize block then run and add the Strings to the ArrayList. The construct is easier to see if the individual braces are put into new lines.

doSomethingWithAnArrayList(new ArrayList() { //creates a new anonymous subclass of ArrayList
     //this is an initialization statement and is run to add the Strings to the ArrayList

After some more researching, however, I don’t really recommend using Double Braces Initialization (like any experienced programmer will tell you) because like all syntatic sugar, the Double Braces Initialization comes with costs:

  1. Whenever you uses Double Braces Initialization in your code, you create an anonymous class. If you use it a lot, your program can get clutter with lots of anonymous classes and become larger than it needed to be. This will also slow your program loading time as it has load more classes and also take up your PermGen (it has something to do with Garbage Collection in Java, it’s not good if you uses too much of it) spaces.
  2. There is a (slight) performance overhead.
  3. Problem with serialization. The outer class will get serialized instead of the inner class.
  4. Wrecks havoc with .equals() and .hashCode(). They won’t work.
  5. This is not well-known and can confuse people who will read your code.

There are also some other problems that can crop up, so just know that while it’s short and sweet, double braces initialization is not really recommended for serious programming. If you really don’t want to use the temporary variables, you can always use the short (and maybe preferred, I don’t know) way:

doSomethingWithAnArrayList( new ArrayList( Arrays.asList("Double", "Braces", "Initialization") ) );

In addition, in Java 7, there may be syntax for easy construction of various types of Collection classes. Look forward to it. Update: Postponed to Java 8.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: