2

In the below piece of code why the getX() method executes first rather than constructor:

public class ConsructorQuestion {

    int x = getX();  // same this.getX();
    public int getX() {
        System.out.println(x + "  via method  ");
        return 10;
    }

    public ConsructorQuestion() {
        System.out.println(x+"  via constructor");
    }

    public static void main(String[] args) {
        ConsructorQuestion t = new ConsructorQuestion();
    }

}
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Bhupesh Kumar
  • 49
  • 1
  • 3
  • 1
    Because that's how class initialization works. Why do you expect otherwise? – shmosel Mar 22 '17 at 07:26
  • 1
    It's actually instance initialization at work here. – Lew Bloch Mar 22 '17 at 07:52
  • Possible duplicate of [How is an instance initializer different from a constructor?](https://stackoverflow.com/questions/1355810/how-is-an-instance-initializer-different-from-a-constructor)? also https://stackoverflow.com/questions/335311/static-initializer-in-java, https://stackoverflow.com/questions/12550135/static-block-vs-initializer-block-in-java, https://stackoverflow.com/questions/6763550/why-java-instance-initializers and questions linked to them. – Oleg Estekhin Sep 13 '17 at 06:54

3 Answers3

6

Instance variables are initialized before the body of the constructor is executed (but after the body of the super class constructor is executed).

Therefore, in your example, int x = getX(); is executed before the constructor body.

This is equivalent to moving the initialization of x to the first line of the constructor:

int x;
public int getX(){
    System.out.println(x+"  via method  ");
    return 10;
}

public ConsructorQuestion() {
    x = getX();
    System.out.println(x+"  via constructor");
}
Eran
  • 387,369
  • 54
  • 702
  • 768
5

Field initializers (and instance initialization blocks) are injected by the compiler into the beginning of every constructor in the class (just after the call to super(...), implicit or explicit) if the constructor doesn't chain to another in the same class (by calling this(...)). So if you look at the bytecode generated, you'll see that your class looks like this:

public class ConsructorQuestion {

    int  x;                                             // ***
    public int getX(){
        System.out.println(x+"  via method  ");
        return 10;
    }

    public ConsructorQuestion() {
        this.x = getX();                                // ***
        System.out.println(x+"  via constructor");
    }

    public static void main(String[] args) {
        ConsructorQuestion t = new ConsructorQuestion();
    }

}

The order in which things are done when constructing instances is covered by JLS§12.5:

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.

Note the order of #4 and #5.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Javac starts from beginning of the class and initializes the class members first and then proceeds with main.

vivekn
  • 35
  • 6
  • 1
    However, what's at issue here is instance initialization, not class initialization, and that means `main` is invoked first. – Lew Bloch Mar 22 '17 at 07:54
  • Java main() is a little different from C/C++ main. In C/C++, apart from global variables, constructor are called post main(). However, in java, class members are initialized in the top-to-bottom order and then comes the main, object constructor. – vivekn Mar 22 '17 at 08:20