In what order are initializer block and variable definitions and etc. executed? (in java)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 01:58:13

The Java Language Specification (section 8.3.2.3) says you can use a variable on the left hand side of an expression, i.e. assign to it, before it is declared, but you cannot use it on the right hand side.

All variables are initialized to their default values, then explicit initializers and anonymous blocks are run in the order they are found in the source file. Finally the constructor is called.

Statics are only run once on the first use of a class.

The compile error appears to be a rule of Java rather than something that necessarily makes sense in every case.

Variable definitions are not done "before" blocks. They are both done at the same time, in the order that they are defined

class SomethingWrongWithMe {
    {
        b = debug("block 1");
    }
    int b = debug("define");
    {
        b = debug("block 2");
    }
    private int debug(String str) {
        System.out.println(str);
        return 0;
    }
}

Output

block 1
define
block 2

First of all, your assumptions are more or less correct, except for the fact that declarations (with initialization, such as int b = 0) and instance initializer blocks are executed in the order they are written.

int b = 0;   // executed first

{
    b = 1;   // executed second
}

int a = b;   // executed third

Also note that the declaration i.e. int b is not executed. The declaration just declares the existence of the variable.

As for the error you got (or, rather the error you didn't get) I agree that it looks strange. I assume that the compiler deals with referencing a variable in an expression and assigning a value to it in different ways. When writing to a variable in an instance initializer, it just checks that the variable is there, while when reading from it, it requires it to be declared above the instance initializer block. I'll see if I can find a reference for that in the JLS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!