【发布时间】:2014-08-29 05:58:02
【问题描述】:
package ali;
public class test {
public static int n = 99;
public static test t1 = new test("t1");
public static test t2 = new test("t2");
public static int i = 0;
public static int j = i;
{
System.out.println("construct block");
}
static {
System.out.println("static construct block");
}
public test(String str){
System.out.println((++j) + ":" + " i="+ i + " n="+n+str);
n++;i++;
}
public static void main(String [] args){
test test1 = new test("initl");
}
}
运行后:
construct block
1: i=0 n=99t1
construct block
2: i=1 n=100t2
static construct block
construct block
1: i=0 n=101initl
谁能告诉我它是如何工作的? 为什么在创建 t1 和 t2 时没有“静态构造块”? 为什么 i 和 j 都改成默认了,而 n 还是不变?
【问题讨论】:
标签: java class initialization