·代码1演示步骤（ch8 目录）

1.定义类InstanceInitializer1

2.定义bool、char等基本类型的实例变量

3.添加main()方法

4.在main()方法中显示定义的实例变量

说明：实例变量在实例初始化时首先被初始化，如果没有被赋初值，则系统将默认的初始值赋给实例变量。

// InstanceInitializer1.java

class InstanceInitializer1

{

boolean bool;

byte by;

char ch;

double d;

float f;

int i;

long l;

short sh;

String str;

public static void main (String [] args)

{

InstanceInitializer1 ii1 = new InstanceInitializer1 ();

System.out.println ("ii1.bool = " + ii1.bool);

System.out.println ("ii1.by = " + ii1.by);

System.out.println ("ii1.ch = " + ii1.ch);

System.out.println ("ii1.d = " + ii1.d);

System.out.println ("ii1.f = " + ii1.f);

System.out.println ("ii1.i = " + ii1.i);

System.out.println ("ii1.l = " + ii1.l);

System.out.println ("ii1.sh = " + ii1.sh);

System.out.println ("ii1.str = " + ii1.str);

}

}

// InstanceInitializer2.java

class InstanceInitializer2

{

boolean bool = true;

byte by = 20;

char ch = 'X';

double d = 8.95;

float f = 2.1f;

int i = 63;

long l = 22L;

short sh = 25000;

String str = "test";

public static void main (String [] args)

{

InstanceInitializer2 ii2 = new InstanceInitializer2 ();

System.out.println ("ii2.bool = " + ii2.bool);

System.out.println ("ii2.by = " + ii2.by);

System.out.println ("ii2.ch = " + ii2.ch);

System.out.println ("ii2.d = " + ii2.d);

System.out.println ("ii2.f = " + ii2.f);

System.out.println ("ii2.i = " + ii2.i);

System.out.println ("ii2.l = " + ii2.l);

System.out.println ("ii2.sh = " + ii2.sh);

System.out.println ("ii2.str = " + ii2.str);

}

}

·代码2演示步骤

1.定义类InstanceInitializer2

2.定义bool、char等基本类型的类变量并给它们赋相应的初值

3.添加main()方法

4.在main()方法中显示定义的实例变量

说明：实例变量在实例初始化时可以赋相应的初值。

// InstanceInitializer3.java

class InstanceInitializer3

{

int instanceField1 = 1;

int instanceField2 = 1 + instanceField1;

public static void main (String [] args)

{

System.out.println (new InstanceInitializer3 ().instanceField2);

}

}

·编码分析

1.定义类InstanceInitializer3

2.定义int类型的类变量classField1并给它们赋相应的初值

3.定义int类型的类变量classField2并利用classField1给它赋初值

4.添加main()方法

5.在main()方法中显示定义的类变量classField1、classField2

说明：实例变量在初始化时可以利用已经赋值的实例变量。

// InstanceInitializer4.java

class InstanceInitializer4

{

int instanceField2 = 1;

int instanceField1 = 1 + instanceField2;

public static void main (String [] args)

{

System.out.println (new InstanceInitializer4 ().instanceField1);

}

}

·编码分析

1.定义类InstanceInitializer4

2.定义int类型的实例变量instanceField1并利用instanceField2给它赋初值

3.定义int类型的实例变量instanceField2并给它们赋相应的初值

4.添加main()方法

5.在main()方法中显示定义的实例变量instanceField1、instanceField2

说明：程序将出现编译错误。注意，实例变量在类初始化时按照从前向后的顺序进行，只能利用已经初始化的实例变量，而不能利用未初始化的实例变量。

// InstanceInitializer5.java

class InstanceInitializer5

{

int instanceField;

{

int hc = hashCode ();

instanceField = hc;

for (int i = 0; i < 32; i++)

{

System.out.print ((hc & 0x80000000) != 0 ? '1' : '0');

hc <<= 1;

}

System.out.println ("");

}

public static void main (String [] args)

{

System.out.println (new InstanceInitializer5 ().instanceField);

System.out.println (new InstanceInitializer5 ().instanceField);

}

}

·编码分析

1.定义类InstanceInitializer5

2.定义实例变量hc

3.定义实例代码段，在代码段中使用实例变量hc

4.添加main()方法

5.在main()方法中显示定义的实例变量hc

说明：实例初始化时先初始化类变量，然后初始化类代码段。调用构造方法后才能调用实例的方法和变量。

// InstanceInitializer6.java

class InstanceInitializer6

{

int instanceField;

{

System.out.println ("Block #1, instanceField = " + instanceField);

int hc = hashCode ();

instanceField = hc;

for (int i = 0; i < 32; i++)

{

System.out.print ((hc & 0x80000000) != 0 ? '1' : '0');

hc <<= 1;

}

System.out.println ("");

}

{

System.out.println ("Block #2, instanceField = " + instanceField);

}

public static void main (String [] args)

{

System.out.println (new InstanceInitializer6 ().instanceField);

System.out.println (new InstanceInitializer6 ().instanceField);

}

}

·编码分析

1.定义类InstanceInitializer6

2.定义实例变量instanceField

3.定义实例代码段，在代码段中使用实例变量instanceField

4.添加main()方法

5.在main()方法中显示定义的类变量instanceField

说明：实例初始化时先初始化实例变量，然后初始化实例代码段。对于实例代码段，执行的顺序是从前到后的。

// InstanceInitializer7.java

class InstanceInitializer7

{

final int instanceField;

{

instanceField = 25;

}

public static void main (String [] args)

{

System.out.println (new InstanceInitializer7 ().instanceField);

}

}

·编码分析

1.定义类InstanceInitializer7

2.定义实例变量instanceField

3.定义实例代码段，在代码段中使用实例变量instanceField

4.添加main()方法

5.在main()方法中显示定义的实例变量instanceField