Why can my instance initializer block reference a field before it is declared?

后端 未结 5 511
無奈伤痛
無奈伤痛 2021-01-18 03:19

My understanding is that you cannot reference a variable before it has been declared, and that all code (including instance initializers) that is within the body of a class,

5条回答
  •  心在旅途
    2021-01-18 04:03

    From docs:

    The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

    The above statement is slightly misleading, because if we follow the explanation of the above doc we can rewrite the original code like this:

    public class WrongVersionOfWhyIsThisOk {
    
        int a = 10;
    
        public WhyIsThisOk (){
            a = 5;
        }
    
        public static void main(String[] args){
            WrongVersionOfWhyIsThisOk why = new WrongVersionOfWhyIsThisOk ();
            System.out.println(why.a);
        }
    }
    

    But running WrongVersionOfWhyIsThisOk will produce 5 instead of 10 that original code produces.

    But in reality it is both the initializer block and variable assignment are copied into constructor:

    public class RightVersionOfWhyIsThisOk {
    
        int a;
    
        public RightVersionOfWhyIsThisOk (){
            a = 5;
            a = 10;
        }
    
        public static void main(String[] args){
            RightVersionOfWhyIsThisOk why = new RightVersionOfWhyIsThisOk ();
            System.out.println(why.a);
        }
    }
    

    Update:

    Here is the doc describing in detail the initialization order and constructor invocation:

    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.

提交回复
热议问题