I have recently found what appears to me to be a new syntax for statically initializing an ArrayList:
new ArrayList() {{ add("first"); add("second"); }};

My question is, what is really happening there? Is that a shortcut for defining a static block (I thought it would need the static keyword)? Or just a way to define a default constructor? Something else? What version of Java did this become valid?

An explanation plus a link to further reading would be greatly appreciated.

edit: My test class for showing whether initializer block executes before or after the constructor is below. Results show that initializer blocks execute before the other constructor code:

import org.junit.Test;

public class InitializerBlockTest {
    class InitializerTest {
        System.out.println("Running initalizer block");

        public InitializerTest() {
            System.out.println("Running default constructor");

    class SubClass extends InitializerTest {
        System.out.println("Running subclass Initializer block");

      public SubClass()  {
        System.out.println("Running subclass constructor");

    public void testIt() {
        new SubClass();


Running initalizer block
Running default constructor
Running subclass Initializer block
Running subclass constructor
  • Where did you find this new syntax? Do you have a link to an article or something to share? – romacafe Dec 8 '10 at 20:05
  • BTW: to add to the confusion, you can have any number of static { } and { } blocks anywhere in your class (outside method definitions of course) – Peter Lawrey Dec 8 '10 at 20:13
  • Related: stackoverflow.com/questions/924285/… It's by the way not new. Has already been in Java for ages. It's indeed very rarely used, and with a good reason. – BalusC Dec 8 '10 at 20:19
  • See stackoverflow.com/questions/1372113/… and others. Difficult one to google. – Tom Hawtin - tackline Dec 8 '10 at 20:19
  • Note, your test verifies that the initializer runs before the constructor, but provides no evidence as to when the super() call runs relative to the initializer. – Lawrence Dol Dec 8 '10 at 20:42

You are creating a new anonymous subclass of ArrayList, with an instance initializer which calls add() twice.

It's the same as:

class MyList extends ArrayList

{ // This is an instance initializer; the code is invoked before the constructor.

public MyList() {
    // I believe initializers run here, but I have never specifically tested this


List list=new MyList();

Note that, personally, I do not advise it as an idiom, since it will lead to class-file explosion.

  • Would anyone care to comment on whether the initializer is invoked before or after the super() invocation? – Lawrence Dol Dec 8 '10 at 20:10
  • It would have to be after as super() as always called first. – Peter Lawrey Dec 8 '10 at 20:12
  • Thanks for the fast answer. Regarding the before/after, I built a quick test class, and it runs the initializer block before. – Gus Dec 8 '10 at 20:30
  • Perhaps you mean, "advise against it"? – I82Much Dec 8 '10 at 21:18
  • @I82Much: Yes, that is what I meant; fingers won't type what the brain is thinking. – Lawrence Dol Dec 8 '10 at 21:24

It is an initializer block for instance variables.

From Oracle's documentation:



    // whatever code is needed for initialization goes here


See: http://download.oracle.com/javase/tutorial/java/javaOO/initial.html


When you write new ArrayList() { } you are creating an anonymous subclass of ArrayList. The { } as in the innermost brackets in your code denote an initializer block and is actually copied into every constructor.

EDIT: You guys sure answer fast!

Your Answer

