package com.zxl.staticdemo; public class BlockTest { static { System.out.println("BlockTest静态代码块执行"); } { System.out.println("BlockTest构造代码块执行"); } public BlockTest(){ System.out.println("BlockTest无参构造执行了"); } public static void main(String[] args) { System.out.println("BlockTest的主函数执行了"); Coder c = new Coder(); Coder c2 = new Coder(); } } class Coder { { System.out.println("Coder构造代码块执行"); } static { System.out.println("Coder静态代码块执行"); } public Coder() { System.out.println("Coder无参空构造执行"); } }
执行顺序: