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

前端 未结 6 1858
滥情空心
滥情空心 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:07

    Case 1 :

    Gives you a compile-error,

    Because at System.out.println(x);

    you are trying to print x which was never initialized.

    Case 2:

    Works because you are not directly using any literal values, instead you are calling some method, which is correct.

    General Rule is,

    If you are trying to access any variable which is never initialized then it will give a compilation error.

提交回复
热议问题