直接看代码

public class BlockAndMain {

    public static void main(String[] args) {
	    System.out.println("我是main方法");
    }

    static {
	    System.out.println("我是静态块");
    }

}

我们运行看看结果

静态初始化块和main方法哪个先被执行?
可以看到是静态块先于main方法,并且与静态块在程序中的位置(在main方法前面和后面)无关

相关文章:

  • 2021-06-16
  • 2022-01-26
  • 2021-08-13
  • 2022-12-23
  • 2021-11-26
  • 2021-11-27
  • 2022-12-23
  • 2021-08-16
猜你喜欢
  • 2021-09-28
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2021-08-15
相关资源
相似解决方案