-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 ?

4
  • 1
    { System.out.println("Constructor Instance Block"); } is not an initialization block. It's just a random code block in the constructor. Commented Dec 20, 2018 at 6: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.) Commented Dec 20, 2018 at 6: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. Commented Dec 20, 2018 at 6: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? Commented Dec 20, 2018 at 6:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

So, you mean there is not concept of instance block and static block within the constructor?
Exactly. They are used within a class, not a method(a constructor is also a method.)
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?
As far as I know, they can only be used within a class.
@Sunny Khan You can read further here : stackoverflow.com/questions/70324/…
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.