如果类X的静态初始化中调用了类Y的静态方法,而类Y的静态初始化块中由调用了类X的静态方法,这种情况叫循环静态初始化,请分析这种情况下类X和类Y的初始化过程。
------解决方案--------------------
看你先加载哪个类
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class A1 {
public A1() {
}
static {
B1.testB1();
}
public static void testA1(){
System.out.println( "testA1 ");
}
public static void main(String[] args){
}
}
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class B1 {
public B1() {
}
static {
A1.testA1();
}
public static void testB1() {
System.out.println( "testB1 ");
}
public static void main(String[] args){
}
}
执行下就知道了.