1

Consider following eg.

public class H{
    int a;

    public H(){
        a=5;
    }

    public H(String x){
        this();
    }

    {a=0;}

    public static void main(String []a){
        System.out.print(new H("").a);
    }
}

The O/P is 5 that means initialize block executes first before this(), but according to Why do this() and super() have to be the first statement in a constructor? first statement should be this(). How is that happened..???

Community
  • 1
  • 1
sagar
  • 725
  • 2
  • 13
  • 30

2 Answers2

4

If you know about constructor-chaining

First new H("") calls your parameterized- constructor there it finds this() so call goes to non-parameterized constructor there the initializer is executed first and a is intialized to zero then again your a =5 sets a to 5 and finally prints 5

SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • See http://stackoverflow.com/questions/19561332/what-is-order-of-execution-of-static-block-initialisatin-block-when-we-using-inh – Hannes Sep 02 '14 at 05:11
3

The JLS is very specific at this (emphasis mine):

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

The bold text in section 2 means that when it executes this, it will again invoke these five steps on the call of such constructor. Since you call this() without arguments, it will follow these steps, but step 2 won't be executed since there's no call to this(), instead it will perform steps 3 and 4, and in step 4 it will execute the instance initializers and instance variable initializers for the class. Then the control will go back to the execution of the parameterless constructor which executes a = 5.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332