关于static语句,构造代码块,构造方法的执行顺序

class OrderTest {
	public static void main(String[] args) {
		System.out.println("main-start");
		new OrderTest();
		System.out.println("main-end");
	}
	
	public static int   method() {
		System.out.println("=====method========");
		return 0;
	}
	{
		System.out.println("__构造块2start__");
	}
	
	static {
		System.out.println("==静态块1start==");
	}
	public static OrderTest t1 = new OrderTest();
	static {
		System.out.println("==静态块2start==");
	}
	public static OrderTest t2  = new OrderTest();

	public static int x =  method();
	static {
		System.out.println("==静态块3start==");
	}

	public OrderTest() {
		System.out.println("=======构造器-start========");
	}
	{
		System.out.println("__构造块1start__");
	}
}


执行结果:

==静态块1start==
__构造块2start__
__构造块1start__
=======构造器-start========
==静态块2start==
__构造块2start__
__构造块1start__
=======构造器-start========
=====method========
==静态块3start==
main-start
__构造块2start__
__构造块1start__
=======构造器-start========
main-end

执行顺序:类加载之后,按从上到下执行被static修饰的语句, 如果有语句new了自身的对象,将从上到下执行构造代码块,构造器,创建完对象后,接着执行下面的static语句,

当static语句执行完之后,再执行main方法.

理所当然的,如果public static OrderTest t1 = new OrderTest();这样的语句放在其它static语句块之前,最先执行的将是构造代码块,构造器.然后才是静态代码块


你可能感兴趣的:(java)