1,static 静态块,构造块的执行顺序
静态代码块:static{}
随类的加载而执行,只执行一次并且优先于main函数,优先于构造函数。用于给类进行初始化
构造函数 ClassName(){}
在对象一建立就执行,给对象进行初始化。
构造代码块
在对象已建立就执行,优先于构造函数执行,用于定义不同对象同性的初始化内容
2,总结
A.构造函数给对应的对象初始化,构造代码块给所有的对象进行统一的初始化。
B执行顺序 静态代码块》构造代码块》构造函数
C 什么时候加载类 :使用类中的内容时会加载。如创建对象,使用类中静态成员,在命令行中运行
3,扩展 关于static
A。声明为static的方法有以下几条限制:
- 它们仅能调用其他的static 方法。
- 它们只能访问static数据。
- 它们不能以任何方式引用this 或super(关键字super 与继承有关,在下一章中描述)。
B.