Java笔记—初始化

来源:互联网 发布:电话属于网络 编辑:程序博客网 时间:2024/04/30 07:43

整理自Java在线文档

Three ways to initialize a data field:

  • By setting a value in a constructor
  • By assigning a value in the declaration
  • Initialization blocks

What happens in detail when a constructor is called:

  • All date fields are initialized to their default value(0, false, or null)
  • All field initializers and initialization blocks are executed, in the order in which they occur in the class declaration
  • If the first line of the constructor calls a second constructor, then the body of the second constructor is executed
  • The body of the constructor is executed

 All static field initializers and static initialization blocks are executed in the order in which they occur in the class declaration.


public class Hello{

static{

System.out.println("Hello World!");

}

}

原创粉丝点击