すべてのコードは Java です。
public class TestLocal
{
public static void main(String [] args)
{
int x;
if (args[0] != null)
{ // assume you know this will
// always be true
x = 7; // statement will run
}
int y = x; // the compiler will choke here
}
}
それで、私の質問は、なぜコンパイラがここでチョークするのですか? if ステートメントをバイパスしますか (これは非常に恐ろしく見えます...) if ステートメント ブロックの外側で x を初期化すると、次のコードのように、コンパイラは文句を言いません。
public class TestLocal
{
public static void main(String [] args)
{
int x;
x = 7; // compiler is happy now..:)
if (args[0] != null)
{ // assume you know this will
// always be true
// statement will run
}
int y = x; // the compiler not complain here now.
}
}
コンパイラのこのような恐ろしい動作はなぜですか?