初期化が行われる順序を理解するのに問題があります。これは私が仮定した順序です:
*Once per
1. Static variable declaration
2. Static block
*Once per object
3. variable declaration
4. initialization block
5. constructor
しかし、このコードによると、私は明らかに間違っています:
class SomethingWrongWithMe
{
{
b=0; //no. no error here.
int a = b; //Error: Cannot reference a field before it is defined.
}
int b = 0;
}
そして、これを行うとエラーが消えます:
class SomethingWrongWithMe
{
int b = 0;
{
b=0;
int a = b; //The error is gone.
}
}
エラーが出ない理由がわかりません
b=0;