Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In context to my previous question Java classes and static blocks what if I changed my code from static block and variables to normal Instance Initialization Block and instance variables. Now how would the code be executed?

class extra3 {
    public static void main(String string[]) {
        Hello123 h = new Hello123();
        System.out.println(h.a);
    }
}
class Hello123 {
    {
        a=20;
    }
    int a=158;
}

Here I am getting output as 158. I am not able to understand the reason here.And the other code being this:

class extra3 {
    public static void main(String string[]) {
        Hello123 h = new Hello123();
        System.out.println(h.a);
    }
}
class Hello123 {
    int a=158;
    {
        a=20;
    }
}

Here the output is 20 which is acceptable because when object is created Instance block is executed first. But why is the output in first code coming as 158?

share|improve this question
    
What I can see, Code executed serially and that is how it should be. –  Subhrajyoti Majumder Jul 23 '14 at 8:47

3 Answers 3

up vote 4 down vote accepted

This is the order of initialization

  1. Set fields to default initial values (0, false, null)
  2. Call the constructor for the object (but don't execute the body of the constructor yet)
  3. Invoke the constructor of the superclass
  4. Initialize fields using initializers and initialization blocks
  5. Execute the body of the constructor

So, when you are initializing fields, both inline initializer (a = 158) and initialization blocks (a = 20) are executed in the order as they have defined.

So in the first case, inline initializer executed after the initialization block, you get 158 as result.

In the second case, initialization block got executed after inline initializer.

share|improve this answer
1  
Thanks a lot. Nice explanation. –  Shashank Agarwal Jul 23 '14 at 9:20
    
I still have 1 doubt. Why did 'a' compile when surely 'a' hasn't been declared when it is set in the instance block –  Shashank Agarwal Jul 23 '14 at 10:17
    
@ShashankAgarwal : I don't understand your question –  Abimaran Kugathasan Jul 23 '14 at 10:22
    
@ShashankAgarwal - 'a' is compiled properly because when constructor is called, the instance variable are set to default values in the step 1 itself. See the "accepted answer" ordering. 'a' is set to 0 , then to 20 and then to 158 in example 1 –  javadg Mar 21 at 18:46

Order matters.

Initialization's and static blocks executes based on the order they placed in source code. That's the reason.

share|improve this answer

Static blocks are executed in the order they are declared in code. This article will help you understand the order of execution of static and non static initlaization blocks

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.