public class shunXu { private static shunXu d = new shunXu(); private SubClass t = new SubClass(); public shunXu(){ System.out.println(4); } public static void main (String [] args){ System.out.println("hello word"); } } class SuperClass{ SuperClass(){ System.out.println("构造方法SuperClass"); } } class SubClass extends SuperClass{ static{ System.out.println(1); } public SubClass(){ System.out.println(2); } }
运行结果:
1
构造方法SuperClass
2
4
hello word
static 修饰的总是伴随着类一块运行,t在d之前运算,因为d运行的时候t还没结束。
private static shunXu d = new shunXu(); private SubClass t = new SubClass();