5.7 构造器初始化
返回
5.7.1 初始化顺序
5.7.2 静态数据初始化
5.7.3 显式的静态初始化
5.7.4 非静态实例初始化
- 可以使用构造器进行初始化
- 无法阻止自动初始化,它将在构造器被调用之前发生
5.7.1 初始化顺序
- 在类的内部,变量定义的先后顺序决定了初始化的顺序.
- 即使变量定义散布于方法定义之间,它们仍旧会在任何方法被调用之前得到初始化
//:initialization/OrderOfInitialization.java
//When the constructor is coalled to create a
//Window object,you'll see a message
class Window{
Window(int mark){ System.out.println("Windows("+mark+")");}
}
class House{
Window w1=new Window(1); //Before constructor
House(){
System.out.println("House()");
w3=new Window(33); //Reinitialize w3
}
Window w2=new Window(2); //After constructor
void f(){System.out.println("f()");}
Window w3=new Window(3); //At the end
}
public class OrderOfInitialization{
public static void main(String[] args) {
House h=new House();
h.f();
}
}/*OutPut:
Windows(1)
Windows(2)
Windows(3)
House()
Windows(33)
f()
*///:~
5.7.2 静态数据初始化
- 无论创建多少对象,静态数据都只占用一份存储区域
- Static关键字只能作用于域
- 未初始化的基本类型域,将获得基本类型的标准初值
- 对象引用,默认处置为null
//:initialization/StaticInitialization.java // Specifying initial values a class definition class Bowl{ Bowl(int mark){System.out.println("Bow("+mark+")");} void f_Bowl(int mark){System.out.println("f_Bowl(" + mark +")");} } class Table { static Bowl bowl1=new Bowl(1); Table(){ System.out.println("Table()"); bowl2.f_Bowl(1); } void f_Table(int mark){System.out.println("f_Table("+mark+")");} static Bowl bowl2=new Bowl(2); } class Cupboard{ Bowl bowl3=new Bowl(3); static Bowl bowl4=new Bowl(4); Cupboard(){ System.out.println("Cupboard()"); bowl4.f_Bowl(2); } void f_Cupboard(int mark){System.out.println("f_Cupboard("+mark+")");} static Bowl bowl5=new Bowl(5); } public class StaticInitialization{ public static void main(String[] args) { System.out.println("Creating a new Cupboard in main"); new Cupboard(); System.out.println("Creating a new Cupboard in main"); new Cupboard(); table.f_Table(1); cupboard.f_Cupboard(1); } static Table table=new Table(); static Cupboard cupboard=new Cupboard(); }/*Output: Bow(1) Bow(2) Table() f_Bowl(1) Bow(4) Bow(5) Bow(3) Cupboard() f_Bowl(2) Creating a new Cupboard in main Bow(3) Cupboard() f_Bowl(2) Creating a new Cupboard in main Bow(3) Cupboard() f_Bowl(2) *///:~
- 初始化顺序:先静态,然后“非静态”
5.7.3 显示的静态初始化
- Java允许将多个静态初始化动作组成一个特殊的”静态子句”(也称”静态块”)
//:initialization/Spoon.java
public Spoon{
static int i;
static { // static block (static sentences)
i=11;
}
}///:~
- 静态块和其它静态初始化一样,只执行一次:当首次生成这个类的一个对象时,或者首次访问属于那个类的静态数据成员时
//: initialization/ExplicitStatic.java
class Cup{
Cup(int marker){
System.out.println("Cup("+marker+")");
}
void f_Cup(int marker){
System.out.println("f_Cup("+marker+")");
}
}
class Cups{
static Cup cup1;
static Cup cup2;
static { //The static initialization only run for one time
cup1=new Cup(1);
cup2=new Cup(2);
}
Cups(){
System.out.println("Cups()");
}
}
public class ExplicitStatic{
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.cup1.f_Cup(99);
}
}/*Output:
Inside main()
Cup(1)
Cup(2)
f_Cup(99)
*///:~
5.7.4 非静态实例初始化
//:initialization/Mugs.java
// Java "Instance initialization"
class Mug{
Mug(int maker){
System.out.println("Mug("+maker+")");
}
void f_Mug(int marker){
System.out.println("f_Mug("+marker+")");
}
}
public class Mugs{
Mug mug1;
Mug mug2;
//this way of initialization is
//necessarily for the initialize of anumouns inner-class
{
mug1=new Mug(1);
mug2=new Mug(2);
System.out.println("mug1 & mug2 initialized");
}
Mugs(){
System.out.println("Mugs()");
}
Mugs(int i){
System.out.println("Mug(int)");
}
public static void main(String[] args) {
System.out.println("Inside main()");
new Mugs();
System.out.println("new Mugs() completed");
new Mugs();
System.out.println("new Mugs() completed");
}
}/*Output:
Inside main()
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs()
new Mugs() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs()
new Mugs() completed
*///:~