public class StaticVariableTest
{
private static StaticVariableTest svt = new StaticVariableTest();//语句(1)
private static int count1;//语句(2)
private static int count2 = 0;//语句(3)
private StaticVariableTest()//语句(4)
{
count1++;
count2++;
}
}
能不能这样说,类在被装载时上面的语句(1)--语句(3)的变量的值分别是null,0,0.不管有没有赋值“=”运算符。等到对象实例化时先执行语句1,再执行语句4;然后执行语句2,最后是语句3.可以这样说吗?
{
private static StaticVariableTest svt = new StaticVariableTest();//语句(1)
private static int count1;//语句(2)
private static int count2 = 0;//语句(3)
private StaticVariableTest()//语句(4)
{
count1++;
count2++;
}
}
能不能这样说,类在被装载时上面的语句(1)--语句(3)的变量的值分别是null,0,0.不管有没有赋值“=”运算符。等到对象实例化时先执行语句1,再执行语句4;然后执行语句2,最后是语句3.可以这样说吗?
首先装载这个类,为静态变量分配内存并设置默认值(svt = null;count1 = 0;count2 = 0);
然后进行类初始化,按代码中声明顺序进行静态变量的初始化