20

Just wondering about the reason of compiling code like this:

class MyClass extends AnotherClass {
  {
    MySecondClass object = new MySecondClass();
    object.doSomething();
  }
}

Whats the difference between this code and code in constructor? This code executes before the object creation.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
Oldestkon
  • 251
  • 2
  • 7
  • I've been wondering for a while about this, I recall having seen this a while ago... anyone know what this structure is called? – SimonT Jun 08 '13 at 15:54
  • 1
    It's called an "initializer block." It's actually just copied into every constructor. – wchargin Jun 08 '13 at 16:02
  • 1
    This question may be duplicate question: http://stackoverflow.com/questions/16128076/instance-initialization-block-and-subclasses – Rong Nguyen Jun 08 '13 at 16:05
  • 2
    @Tangmeister It's called an *instance initialization block*. Unlike a *static initialization block* it doesn't use any keyword to prefix the block and hence looks confusing. – Ravi K Thapliyal Jun 08 '13 at 16:06
  • If you have proper constructor chaining you can avoid having initialization block. – Suresh Atta Apr 12 '16 at 10:26

3 Answers3

23

The code inside the braces with no names will be part of the constructor of the class and be executed before the logic contained in the class constructor.

Quick example:

public class Foo {
    {
        System.out.println("Before Foo()");
    }

    public Foo() {
        System.out.println("Inside Foo()");
    }

    {
        System.out.println("Not After Foo()");
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 5
    It goes before every constructor. (`javap -c` will tell you that.) – millimoose Jun 08 '13 at 15:37
  • Oh, thank you very much. Just was playing with code and suddenly realized that code like this can compile. It reminds me school-program Pascal :D – Oldestkon Jun 08 '13 at 15:42
  • 1
    With an instance initializer, you can just write the code once, and it will be executed no matter what constructor is used to create the object. – Miuler Apr 12 '14 at 05:31
13

This is called an instance initializer. The code in the initializer is inserted after the call to the super class constructor and before the rest of the constructor code.

The first operation of any constructor is to invoke a super class constructor. If a constructor is called explicitly, super(...), the specified constructor is used. If no constructor is explicitly invoked, the default constructor (with no arguments) is invoked in the super class. If no such constructor exists, it is a compile time error.

After this explicit or implicit constructor invocation, instance initializers are invoked in the order they appear in source code (yes, you can have more than one initializer).

To illustrate, running this program prints

Another constructor
Init 1
Init 2
Test constructor
class Another {
  Another() { System.out.println("Another constructor"); }
}

class Test extends Another {

  public static void main(String[] args) { new Test(); }

  { System.out.println("Init 1"); }

  Test() { System.out.println("Test constructor"); }

  { System.out.println("Init 2"); }

}

The most commonly seen application is in the initalization the "double brace initialization" idiom, where an anonymous inner class is defined, and an instance is created and configured at once. Here's a fairly common example from Swing programming:

JButton popupButton = new JButton(new AbstractAction("Popup") {
  {
    putValue(Action.SHORT_DESCRIPTION, "Popup a dialog");
  }

  @Override
  public void actionPerformed(ActionEvent evt)
  {
    popup();
  }
});

This could be useful if you have multiple constructors, and need to perform some parameter-less initialization in every constructor. This could be factored into an initialization block.

Community
  • 1
  • 1
erickson
  • 265,237
  • 58
  • 395
  • 493
  • This answer greatly explains the order in which instance initializers and constructors are called. Thanks! – Louis CAD Sep 12 '16 at 13:43
3

This is an instance initialization block that runs before the constructor and if you ask why would one wanna use it in place of the constructor? The answer is no, you don't.

Just wondering about the reason of compiling code like this:

You usually use it to factor out common code when using constructor overloading. So, the "the" above actually refers to one of the overloaded constructors that gets called on object instantiation after the common instance initialization code block has executed.

By the way, you could sometimes achieve the same by calling one constructor from the other but the call then has to be on the first line inside the calling constructor or the code won't compile.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • No, of course I wasn't planning to use it instead of constructor. It was just a plain interest, nothing more. And thanks for explanation of constructor overloading. – Oldestkon Jun 08 '13 at 15:51
  • 1
    You're welcome. You'll probably know this already. There are static init blocks too that are used to initialize static class members. And, multiple blocks execute in the order they appear in your code. – Ravi K Thapliyal Jun 08 '13 at 16:01
  • I know the basics already, at the moment I am learning "the depths" of multi-threading, like volatile vars, or synchronized blocks. – Oldestkon Jun 08 '13 at 16:10