Why attempt to print uninitialized variable does not always result in an error message

前端 未结 6 1857
滥情空心
滥情空心 2020-12-24 10:28

Some may find it similar to the SO question Will Java Final variables have default values? but that answer doesn\'t completely solve this, as that question doesn\'t directl

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 11:04

    In the JLS, §8.3.3. Forward References During Field Initialization, its stated that there's a compile-time error when:

    Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. Specifically, it is a compile-time error if all of the following are true:

    • The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;

    • The use is a simple name in either an instance variable initializer of C or an instance initializer of C;

    • The use is not on the left hand side of an assignment;

    • C is the innermost class or interface enclosing the use.

    The following rules come with a few examples, of which the closest to yours is this one:

    class Z {
        static int peek() { return j; }
        static int i = peek();
        static int j = 1;
    }
    class Test {
        public static void main(String[] args) {
            System.out.println(Z.i);
        }
    }
    

    Accesses [to static or instance variables] by methods are not checked in this way, so the code above produces output 0, because the variable initializer for i uses the class method peek() to access the value of the variable j before j has been initialized by its variable initializer, at which point it still has its default value (§4.12.5 Initial Values of Variables).

    So, to summarize, your second example compiles and executes fine, because the compiler does not check if the x variable was already initialized when you invoke printX() and when printX() actually takes place at Runtime, the x variable will be assigned with its default value (0).

提交回复
热议问题