我有问题了解初始化发生的顺序。这是我认为的顺序:初始化程序块和变量定义等以什么顺序执行? (java)
*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;
你使用什么编译器? ORACLE的javac?或者一些IDE(至少Eclipse带有自己的编译器) – 2012-03-12 10:46:48
在这种情况下,Eclipse和Oracles javac的行为相同。 – aioobe 2012-03-12 10:48:12
using eclipse .. – Untitled 2012-03-12 12:55:14