-1

As we know that the instance block is called before the constructor. Specifications, Stack Overflow Answer etc.

So, the output that we expect from the code below:

class Test{
    static { System.out.println("Static Block"); }
    { System.out.println("Instance Block"); }
    Test(){
        System.out.println("Constructor Body");
        { System.out.println("Constructor Instance Block"); }
    }
}
class TestApp{
    public static void main(String[] args){
        new Test();
    }
} 

Should Be:

Static Block
Instance Block

Constructor Instance Block
Constructor Body

Then why i am getting the following output:

Static Block
Instance Block

Constructor Body
Constructor Instance Block

But if i change the order of the statements in constructor like:

class Test{
    static { System.out.println("Static Block"); }
    { System.out.println("Instance Block"); }
    Test(){
        { System.out.println("Constructor Instance Block"); }
        System.out.println("Constructor Body");
    }
}

Then it outputs the expected result but this should not be the way because the docs says that instance block is called before the constructor.

Why the instance block is called after the constructor body or we can say why it is called in order wise ?

Stack Overflow
  • 1
  • 5
  • 23
  • 51
  • 1
    `{ System.out.println("Constructor Instance Block"); }` is not an initialization block. It's just a random code block in the constructor. – ernest_k Dec 20 '18 at 06:39
  • 1
    There is no such concept as a constructor instance block. All code within any method or constructor is executed in order. *(Well, except for some compiler optimizations, but that's another story.)* – MC Emperor Dec 20 '18 at 06:47
  • Instance initializers are not called *before* the constructor. They are inlined into all constructors which invoke `super`, in between the call to `super` and the rest of that constructor's body. – Andy Turner Dec 20 '18 at 06:48
  • Where the `instance blocks` and `static blocks` are applicable and allowed to be used for their real meaning, one place that i know is `class`, are there any other places as well where these are also applicable? – Stack Overflow Dec 20 '18 at 06:48

2 Answers2

3

{ System.out.println("Constructor Instance Block"); } is not an instance block, but yet another common line of code in a method(in your case, the constructor). So it excute AFTER the System.out.println("Constructor Body"); , which is natural.

ZhaoGang
  • 4,491
  • 1
  • 27
  • 39
  • So, you mean there is not concept of `instance block` and `static block` within the `constructor`? – Stack Overflow Dec 20 '18 at 06:41
  • 2
    Exactly. They are used within a class, not a method(a constructor is also a method.) – ZhaoGang Dec 20 '18 at 06:50
  • Please can you tell where the `instance blocks` and `static blocks` are applicable and allowed to be used for their real meaning, one place that i know is in the `class`, are there any other places as well where these are also applicable? – Stack Overflow Dec 20 '18 at 06:53
  • 2
    As far as I know, they can only be used within a class. – ZhaoGang Dec 20 '18 at 06:56
  • @SunnyKhan Also note that they cannot be placed in an inner class, but can be placed in a static nested class(which has a `static` keyword). – ZhaoGang Dec 20 '18 at 07:00
  • @ZhaoGang correct. They can only appear in a [`ClassBodyDeclaration`](https://docs.oracle.com/javase/specs/jls/se9/html/jls-19.html). – Andy Turner Dec 20 '18 at 07:01
  • @ZhaoGang - You mean `class{ class{ ... } }` and `static class{ class{ ... } }`? – Stack Overflow Dec 20 '18 at 07:03
  • 1
    @Sunny Khan You can read further here : https://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class – ZhaoGang Dec 20 '18 at 07:05
1

static initializers and instance initializers are corresponding to class and its creation (see: Static block vs. initializer block in Java?).

That's why it must be placed in the body of the class.

Otherwise (for example, in method body), your {...} will be considered as a block of instructions (as in C language). If you try to put the construction static {...} elsewhere (not in class body) you will get a compilation error.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61