在Java中,类变量在
following order
-
-
该类的所有静态变量都设置为
default values
-
静态变量和静态初始化块,按声明顺序。
-
超类的实例变量
-
该类的所有实例变量都设置为
默认值
-
实例变量和实例级初始化块,按声明顺序
因此,考虑到以下代码:
class Test
extends TestSuper
{
final int ti1;
final int ti2 = counter ++;
{ ti1 = counter ++; }
static final int ts1;
static final int ts2 = counter ++;
static { ts1 = counter ++; }
public static void main(String[] argv) {
Test test1 = new Test();
printTest(test1);
Test test2 = new Test();
printTest(test2);
}
private static void printTest(Test test) {
System.out.print("ss2 = " + test.ss2);
System.out.print(", ss1 = " + test.ss1);
System.out.print(", ts2 = " + test.ts2);
System.out.println(", ts1 = " + test.ts1);
System.out.print("si2 = " + test.si2);
System.out.print(", si1 = " + test.si1);
System.out.print(", ti2 = " + test.ti2);
System.out.println(", ti1 = " + test.ti1);
System.out.println("counter = " + test.counter);
}
}
class TestSuper
{
static int counter = 0;
final int si1;
final int si2 = counter ++;
{ si1 = counter ++; }
static final int ss1;
static final int ss2 = counter ++;
static { ss1 = counter ++; }
}
然后我们得到以下输出:
ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
counter = 8
ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
si2 = 8, si1 = 9, ti2 = 10, ti1 = 11
counter = 12
现在,关于第二个问题,字段的重新排序是否会改变类的行为。是的,通过对字段重新排序,可以更改字段的初始化顺序。现在,在所有字段都是独立的特定情况下,这不会影响观察到的行为,但是只要字段不是独立的,例如在上面的代码中,那么对字段重新排序可能会更改其初始值。
例如,如果三行:
static final int ss1;
static final int ss2 = counter ++;
static { ss1 = counter ++; }
static final int ss1;
static { ss1 = counter ++; }
static final int ss2 = counter ++;
然后输出将更改为:
ss2 = 1, ss1 = 0, ts2 = 2, ts1 = 3
si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
counter = 8
ss2
,及
ss1
原因是该行为在
Java Language Specification