Question: 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.

Answer: 
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()");
    }
}

```
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.