java之例項程式碼塊,程式碼執行順序
阿新 • • 發佈:2020-09-10
/**1.除了類載入程式碼塊之外,還有一種語句塊叫做:例項語句塊
* 2.例項語句塊在類載入是並沒有執行
* 3.例項程式碼塊語法:
* {
* java語句;
* }
* 4.例項語句塊在什麼時候執行?
* 只要是構造方法執行時,就會在構造方法執行前執行例項程式碼塊
* */
public class InstanceCode { public static void main(String[] args) { System.out.println("main begin"); new InstanceCode(); new InstanceCode();new InstanceCode("abc"); new InstanceCode("abc"); } /**例項語句塊 * */ { System.out.println("例項語句執行"); } //Constructor public InstanceCode(){ System.out.println("無引數構造方法"); } //Constructor public InstanceCode(String name){ System.out.println("有引數構造方法"); } }
結果:
main begin
例項語句執行
無引數構造方法
例項語句執行
無引數構造方法
例項語句執行
有引數構造方法
例項語句執行
有引數構造方法
判斷以下程式的執行順序: