以single s2 = new single(0);为例1、装入类,为静态成员分配空间 执行 private static single s = null; private static int i = 0;(static i仅分配一次)执行static single s = new single(10);构造s(仅构造1次)为s的非静态成员ss赋值,执行 private single ss = creates(); 调用 private single creates(){...} 在上述方法内再递归,成功调用10次至i值为11,每次调用i增1,i为static,执行构造方法输出10次“created11”(i的值已递增为11),即逐层退出递归,退出递归后执行构造函数“created10”(传的参数是10)。 2、执行new single(0); 执行 private single ss = creates(); 同上递归调用,只不过调用前i的值已为11,输出10次“created11”。 执行构造方法,输出“created0”(传的参数是0)。
执行single s3 = new single(1);时类已载入,执行上述第2部即可。
2、执行new single(0); 执行 private single ss = creates(); 同上递归调用,只不过调用前i的值已为11,输出10次“created11”。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`` //就是后面10次“created11”不太明白,i不是已经是11了吗。 执行构造方法,输出“created0”(传的参数是0)。
1. 装载类 进入类初始化静态变量 private static single s = new single(10); private static int i = 0;1.1. 当到 private static single s = new single(10); 创建对象, 初始化对象变量 private single ss = creates(); 调用creates(), 递归创建10个对象, 直到i=10;
1.2. 最后从第10个对象调用构建方法single(int i), i=10 打印 created11 然后是第9,8,.....1 一共打印10个111.3. 结束创建静态变量创建 private static single s = new single(10); 打印created101.4. 初始化变量 i 初始化前 i = 10 private static int i = 0; 类初始化完毕 i=02.1 回到主函数的创建, 创建对象 single s2 = new single(0); 创建对象, 初始化对象变量 private single ss = creates(); 如1.1 - 1.22.2 完成初始化对象 打印created102.3 主函数第二个对象创建 single s2 = new single(0); 同样也会调用creates(), 只是由于i=10, 返回null 打印created10难点 初始化类是按顺序执行的, static i = 0;会重新更改i的值, 如果改为 private static int i; 这样结果就没有后来的10个created11了 类被初始化前, 其方法是可以被内部调用的(一段执行代码而以)
created11
created11
created11
created11
created11
created11
created11
created11
created11
created10
created11
created11
created11
created11
created11
created11
created11
created11
created11
created11
created0
created1
真是不可思议static成员反而生成的晚了!!!将构造函数改成这样也无济于事
single(int i){
System.out.println("created" + i);
System.out.flush();
}关注高手解释原因
执行
private static single s = null;
private static int i = 0;(static i仅分配一次)执行static single s = new single(10);构造s(仅构造1次)为s的非静态成员ss赋值,执行
private single ss = creates();
调用
private single creates(){...}
在上述方法内再递归,成功调用10次至i值为11,每次调用i增1,i为static,执行构造方法输出10次“created11”(i的值已递增为11),即逐层退出递归,退出递归后执行构造函数“created10”(传的参数是10)。
2、执行new single(0);
执行
private single ss = creates();
同上递归调用,只不过调用前i的值已为11,输出10次“created11”。
执行构造方法,输出“created0”(传的参数是0)。
执行
private single ss = creates();
同上递归调用,只不过调用前i的值已为11,输出10次“created11”。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``
//就是后面10次“created11”不太明白,i不是已经是11了吗。
执行构造方法,输出“created0”(传的参数是0)。
1. 装载类
进入类初始化静态变量
private static single s = new single(10);
private static int i = 0;1.1. 当到 private static single s = new single(10);
创建对象, 初始化对象变量
private single ss = creates();
调用creates(), 递归创建10个对象, 直到i=10;
1.2. 最后从第10个对象调用构建方法single(int i), i=10
打印 created11
然后是第9,8,.....1
一共打印10个111.3. 结束创建静态变量创建
private static single s = new single(10);
打印created101.4. 初始化变量 i
初始化前 i = 10
private static int i = 0;
类初始化完毕 i=02.1 回到主函数的创建, 创建对象
single s2 = new single(0);
创建对象, 初始化对象变量
private single ss = creates();
如1.1 - 1.22.2 完成初始化对象
打印created102.3 主函数第二个对象创建
single s2 = new single(0);
同样也会调用creates(), 只是由于i=10, 返回null
打印created10难点
初始化类是按顺序执行的, static i = 0;会重新更改i的值, 如果改为 private static int i; 这样结果就没有后来的10个created11了
类被初始化前, 其方法是可以被内部调用的(一段执行代码而以)