views:

383

answers:

4

What does {{...}} block mean in the following code?

class X {
private Y var1;

    private X() {
        Z context = new Z(new SystemThreadPool()) {{
            var1 = new Y();
        }};
    }
}
+10  A: 

It's called double curly brace initialization.

It means you're creating an anonymous subclass and the code within the double braces is basically a constructor. It's often used to add contents to collections because Java's syntax for creating what are essentially collection constants is somewhat awkward.

So you might do:

List<String> list = new ArrayList<String>() {{
  add("one");
  add("two");
  add("three");
}};

instead of:

List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");

I actually don't like that and prefer to do this:

List<String> list = Arrays.asList("one", "two", "three");

So it doesn't make much sense in that case whereas it does for, say, Maps, which don't have a convenient helper.

cletus
Java7 will have "map literal" syntax, so eventually the {{ }} syntax will become obsolete. Assuming anyone actually uses Java7. I may have retired by that point.
skaffman
A: 

It's called Object Initialization which means you're a assigning a value to Y of Z instance.

Braveyard
+3  A: 

The "outer" braces mean that you're making an anonymous subclass, the second braces are the object initializer. The initializer is run before the class' constructor, but after any super calls (and therefore also after any superclass initializers). You can use initializers in non-anonymous classes, too, which is a convenient way to initiate final fields if you have several constructors which cannot call each other, or fields which need more complex initialization than usual field initializers allow.

Consider this class:

class X extends Y{
    private final int lulz;

    private static boolean someCondition(){...}
    private static boolean danger() throws SomeException { ... }
    public X(A a) throws SomeException {
        super(a); 
        lulz = someCondition()? danger() : 0;
    }
    public X(B b) throws SomeException {
        super(b); 
        lulz = someCondition()? danger() : 0;
    }
}

It could be rewritten as:

class X extends Y{
    private final int lulz;

    private static boolean someCondition(){...}
    private static boolean danger() throws SomeException { ... }
    { // initalizer -- might throw SomeException!
        lulz = someCondition()? danger() : 0;
    }
    public X(A a) throws SomeException { super(a); }
    public X(B b) throws SomeException { super(b); }
}

If the initializer can throw a checked exception, all constructors must declare they can throw it.

gustafc
A: 

You are creating an anonymous class and using the class Instance initializer idiom, like this:

class X {
    private Y var1;

    private X() {
        Z context = new Z(
               new SystemThreadPool()) {
                   {                        // This is the initialize idiom
                       var1 = new Y();      //
                   }                        //
               }
          );  // BTW you are missing ")"
    }
}
David Santamaria