java的静态代码块,构造代码块, 构造方法的执行顺序

java的执行顺序为:1.静态代码块 ;2.构造代码块;3构造方法。

 例子:

package testreflect;

/**
*类描述:静态代码块在类被加载时候加调用;内部类在静态代码块之后;最后才是构造方法(先父类后子类)
*@author: 张宇
*@date: 日期: 2018年8月30日 时间: 下午4:43:58
*@version 1.0
 */



class Parent{
	
	static{
		System.out.println("Parent static code!");
	}
	
	{
		System.out.println("Parent inner Code!");
	}
	
	public Parent(){
		System.out.println("Parent constructor!!!");
	}
}

class Son extends Parent{
	static{
		System.out.println("Son static Code!");
	}
	
	{
		System.out.println("Son inner code!");
	}
	
	public Son(){
		System.out.println("Son constructor!");
	}
}

public class LoadTest {
    public static void  main(String []args){
    	new Son();
    }
}

运行结果:

Parent static code!
Son static Code!
Parent inner Code!
Parent constructor!!!
Son inner code!
Son constructor!

猜你喜欢

转载自blog.csdn.net/zy345293721/article/details/82224221