static块、构造块、构造方法运行顺序以及执行次数

 

代码
public class Block
{
static
{
System.
out.println("static块");
}

{
System.
out.println("构造块");
}

Block()
{
System.
out.println("构造方法!!!");
}
public static void main(String[] args)
{
new Block();
new Block();
new Block();
}
}
/*
//output:
static块
构造块
构造方法!!!
构造块
构造方法!!!
构造块
构造方法!!!
*/
//~从打印结果来看,静态块只会在类加载的时候执行一次。并且在构造方法之前执行。
//~

 

 

相关文章:

  • 2022-12-23
  • 2022-02-28
  • 2022-03-06
  • 2022-12-23
  • 2021-05-31
  • 2021-08-31
  • 2021-08-20
猜你喜欢
  • 2022-01-14
  • 2021-07-03
  • 2021-08-09
  • 2022-12-23
  • 2021-07-07
  • 2022-12-23
  • 2021-06-23
相关资源
相似解决方案