4.4 成员初始化

Java尽自己的全力保证所有变量都能在使用前得到正确的初始化。若被定义成相对于一个方法的“局部”变量,这一保证就通过编译期的出错提示表现出来。因此,如果使用下述代码:

  1. void f() {
  2. int i;
  3. i++;
  4. }

就会收到一条出错提示消息,告诉你i可能尚未初始化。当然,编译器也可为i赋予一个默认值,但它看起来更象一个程序员的失误,此时默认值反而会“帮倒忙”。若强迫程序员提供一个初始值,就往往能够帮他/她纠出程序里的“Bug”。

然而,若将基本类型设为一个类的数据成员,情况就会变得稍微有些不同。由于任何方法都可以初始化或使用那个数据,所以在正式使用数据前,若还是强迫程序员将其初始化成一个适当的值,就可能不是一种实际的做法。然而,若为其赋予一个垃圾值,同样是非常不安全的。因此,一个类的所有基本类型数据成员都会保证获得一个初始值。可用下面这段小程序看到这些值:

  1. //: InitialValues.java
  2. // Shows default initial values
  3. class Measurement {
  4. boolean t;
  5. char c;
  6. byte b;
  7. short s;
  8. int i;
  9. long l;
  10. float f;
  11. double d;
  12. void print() {
  13. System.out.println(
  14. "Data type Inital value\n" +
  15. "boolean " + t + "\n" +
  16. "char " + c + "\n" +
  17. "byte " + b + "\n" +
  18. "short " + s + "\n" +
  19. "int " + i + "\n" +
  20. "long " + l + "\n" +
  21. "float " + f + "\n" +
  22. "double " + d);
  23. }
  24. }
  25. public class InitialValues {
  26. public static void main(String[] args) {
  27. Measurement d = new Measurement();
  28. d.print();
  29. /* In this case you could also say:
  30. new Measurement().print();
  31. */
  32. }
  33. } ///:~

输入结果如下:

  1. Data type Inital value
  2. boolean false
  3. char
  4. byte 0
  5. short 0
  6. int 0
  7. long 0
  8. float 0.0
  9. double 0.0

其中,Char值为空(NULL),没有数据打印出来。

稍后大家就会看到:在一个类的内部定义一个对象引用时,如果不将其初始化成新对象,那个引用就会获得一个空值。

4.4.1 规定初始化

如果想自己为变量赋予一个初始值,又会发生什么情况呢?为达到这个目的,一个最直接的做法是在类内部定义变量的同时也为其赋值(注意在C++里不能这样做,尽管C++的新手们总“想”这样做)。在下面,Measurement类内部的字段定义已发生了变化,提供了初始值:

  1. class Measurement {
  2. boolean b = true;
  3. char c = 'x';
  4. byte B = 47;
  5. short s = 0xff;
  6. int i = 999;
  7. long l = 1;
  8. float f = 3.14f;
  9. double d = 3.14159;
  10. //. . .

亦可用相同的方法初始化非基本(主)类型的对象。若Depth是一个类,那么可象下面这样插入一个变量并进行初始化:

  1. class Measurement {
  2. Depth o = new Depth();
  3. boolean b = true;
  4. // . . .

若尚未为o指定一个初始值,同时不顾一切地提前试用它,就会得到一条运行期错误提示,告诉你产生了名为“异常”(Exception)的一个错误(在第9章详述)。 甚至可通过调用一个方法来提供初始值:

  1. class CInit {
  2. int i = f();
  3. //...
  4. }

当然,这个方法亦可使用参数,但那些参数不可是尚未初始化的其他类成员。因此,下面这样做是合法的:

  1. class CInit {
  2. int i = f();
  3. int j = g(i);
  4. //...
  5. }

但下面这样做是非法的:

  1. class CInit {
  2. int j = g(i);
  3. int i = f();
  4. //...
  5. }

这正是编译器对“向前引用”感到不适应的一个地方,因为它与初始化的顺序有关,而不是与程序的编译方式有关。

这种初始化方法非常简单和直观。它的一个限制是类型Measurement的每个对象都会获得相同的初始化值。有时,这正是我们希望的结果,但有时却需要盼望更大的灵活性。

4.4.2 构造器初始化

可考虑用构造器执行初始化进程。这样便可在编程时获得更大的灵活程度,因为我们可以在运行期调用方法和采取行动,从而“现场”决定初始化值。但要注意这样一件事情:不可妨碍自动初始化的进行,它在构造器进入之前就会发生。因此,假如使用下述代码:

  1. class Counter {
  2. int i;
  3. Counter() { i = 7; }
  4. // . . .

那么i首先会初始化成零,然后变成7。对于所有基本类型以及对象引用,这种情况都是成立的,其中包括在定义时已进行了明确初始化的那些一些。考虑到这个原因,编译器不会试着强迫我们在构造器任何特定的场所对元素进行初始化,或者在它们使用之前——初始化早已得到了保证(注释⑤)。

⑤:相反,C++有自己的“构造器初始模块列表”,能在进入构造器主体之前进行初始化,而且它对于对象来说是强制进行的。参见《Thinking in C++》。

(1) 初始化顺序

在一个类里,初始化的顺序是由变量在类内的定义顺序决定的。即使变量定义大量遍布于方法定义的中间,那些变量仍会在调用任何方法之前得到初始化——甚至在构造器调用之前。例如:

  1. //: OrderOfInitialization.java
  2. // Demonstrates initialization order.
  3. // When the constructor is called, to create a
  4. // Tag object, you'll see a message:
  5. class Tag {
  6. Tag(int marker) {
  7. System.out.println("Tag(" + marker + ")");
  8. }
  9. }
  10. class Card {
  11. Tag t1 = new Tag(1); // Before constructor
  12. Card() {
  13. // Indicate we're in the constructor:
  14. System.out.println("Card()");
  15. t3 = new Tag(33); // Re-initialize t3
  16. }
  17. Tag t2 = new Tag(2); // After constructor
  18. void f() {
  19. System.out.println("f()");
  20. }
  21. Tag t3 = new Tag(3); // At end
  22. }
  23. public class OrderOfInitialization {
  24. public static void main(String[] args) {
  25. Card t = new Card();
  26. t.f(); // Shows that construction is done
  27. }
  28. } ///:~

Card中,Tag对象的定义故意到处散布,以证明它们全都会在构造器进入或者发生其他任何事情之前得到初始化。除此之外,t3在构造器内部得到了重新初始化。它的输入结果如下:

  1. Tag(1)
  2. Tag(2)
  3. Tag(3)
  4. Card()
  5. Tag(33)
  6. f()

因此,t3引用会被初始化两次,一次在构造器调用前,一次在调用期间(第一个对象会被丢弃,所以它后来可被当作垃圾收掉)。从表面看,这样做似乎效率低下,但它能保证正确的初始化——若定义了一个重载的构造器,它没有初始化t3;同时在t3的定义里并没有规定“默认”的初始化方式,那么会产生什么后果呢?

(2) 静态数据的初始化

若数据是静态的(static),那么同样的事情就会发生;如果它属于一个基本类型,而且未对其初始化,就会自动获得自己的标准基本类型初始值;如果它是指向一个对象的引用,那么除非新建一个对象,并将引用同它连接起来,否则就会得到一个空值(NULL)。

如果想在定义的同时进行初始化,采取的方法与非静态值表面看起来是相同的。但由于static值只有一个存储区域,所以无论创建多少个对象,都必然会遇到何时对那个存储区域进行初始化的问题。下面这个例子可将这个问题说更清楚一些:

  1. //: StaticInitialization.java
  2. // Specifying initial values in a
  3. // class definition.
  4. class Bowl {
  5. Bowl(int marker) {
  6. System.out.println("Bowl(" + marker + ")");
  7. }
  8. void f(int marker) {
  9. System.out.println("f(" + marker + ")");
  10. }
  11. }
  12. class Table {
  13. static Bowl b1 = new Bowl(1);
  14. Table() {
  15. System.out.println("Table()");
  16. b2.f(1);
  17. }
  18. void f2(int marker) {
  19. System.out.println("f2(" + marker + ")");
  20. }
  21. static Bowl b2 = new Bowl(2);
  22. }
  23. class Cupboard {
  24. Bowl b3 = new Bowl(3);
  25. static Bowl b4 = new Bowl(4);
  26. Cupboard() {
  27. System.out.println("Cupboard()");
  28. b4.f(2);
  29. }
  30. void f3(int marker) {
  31. System.out.println("f3(" + marker + ")");
  32. }
  33. static Bowl b5 = new Bowl(5);
  34. }
  35. public class StaticInitialization {
  36. public static void main(String[] args) {
  37. System.out.println(
  38. "Creating new Cupboard() in main");
  39. new Cupboard();
  40. System.out.println(
  41. "Creating new Cupboard() in main");
  42. new Cupboard();
  43. t2.f2(1);
  44. t3.f3(1);
  45. }
  46. static Table t2 = new Table();
  47. static Cupboard t3 = new Cupboard();
  48. } ///:~

Bowl允许我们检查一个类的创建过程,而TableCupboard能创建散布于类定义中的Bowlstatic成员。注意在static定义之前,Cupboard先创建了一个非staticBowl b3。它的输出结果如下:

  1. Bowl(1)
  2. Bowl(2)
  3. Table()
  4. f(1)
  5. Bowl(4)
  6. Bowl(5)
  7. Bowl(3)
  8. Cupboard()
  9. f(2)
  10. Creating new Cupboard() in main
  11. Bowl(3)
  12. Cupboard()
  13. f(2)
  14. Creating new Cupboard() in main
  15. Bowl(3)
  16. Cupboard()
  17. f(2)
  18. f2(1)
  19. f3(1)

static初始化只有在必要的时候才会进行。如果不创建一个Table对象,而且永远都不引用Table.b1Table.b2,那么static Bowl b1b2永远都不会创建。然而,只有在创建了第一个Table对象之后(或者发生了第一次static访问),它们才会创建。在那以后,static对象不会重新初始化。

初始化的顺序是首先static(如果它们尚未由前一次对象创建过程初始化),接着是非static对象。大家可从输出结果中找到相应的证据。

在这里有必要总结一下对象的创建过程。请考虑一个名为Dog的类:

(1) 类型为Dog的一个对象首次创建时,或者Dog类的static方法/static字段首次访问时,Java解释器必须找到Dog.class(在事先设好的类路径里搜索)。

(2) 找到Dog.class后(它会创建一个Class对象,这将在后面学到),它的所有static初始化模块都会运行。因此,static初始化仅发生一次——在Class对象首次载入的时候。

(3) 创建一个new Dog()时,Dog对象的构建进程首先会在内存堆(Heap)里为一个Dog对象分配足够多的存储空间。

(4) 这种存储空间会清为零,将Dog中的所有基本类型设为它们的默认值(零用于数字,以及booleanchar的等价设定)。

(5) 进行字段定义时发生的所有初始化都会执行。

(6) 执行构造器。正如第6章将要讲到的那样,这实际可能要求进行相当多的操作,特别是在涉及继承的时候。

(3) 明确进行的静态初始化

Java允许我们将其他static初始化工作划分到类内一个特殊的“static构建从句”(有时也叫作“静态块”)里。它看起来象下面这个样子:

  1. class Spoon {
  2. static int i;
  3. static {
  4. i = 47;
  5. }
  6. // . . .

尽管看起来象个方法,但它实际只是一个static关键字,后面跟随一个方法主体。与其他static初始化一样,这段代码仅执行一次——首次生成那个类的一个对象时,或者首次访问属于那个类的一个static成员时(即便从未生成过那个类的对象)。例如:

  1. //: ExplicitStatic.java
  2. // Explicit static initialization
  3. // with the "static" clause.
  4. class Cup {
  5. Cup(int marker) {
  6. System.out.println("Cup(" + marker + ")");
  7. }
  8. void f(int marker) {
  9. System.out.println("f(" + marker + ")");
  10. }
  11. }
  12. class Cups {
  13. static Cup c1;
  14. static Cup c2;
  15. static {
  16. c1 = new Cup(1);
  17. c2 = new Cup(2);
  18. }
  19. Cups() {
  20. System.out.println("Cups()");
  21. }
  22. }
  23. public class ExplicitStatic {
  24. public static void main(String[] args) {
  25. System.out.println("Inside main()");
  26. Cups.c1.f(99); // (1)
  27. }
  28. static Cups x = new Cups(); // (2)
  29. static Cups y = new Cups(); // (2)
  30. } ///:~

在标记为(1)的行内访问static对象c1的时候,或在行(1)标记为注释,同时(2)行不标记成注释的时候,用于Cupsstatic初始化模块就会运行。若(1)和(2)都被标记成注释,则用于Cupsstatic初始化进程永远不会发生。

(4) 非静态实例的初始化

针对每个对象的非静态变量的初始化,Java 1.1提供了一种类似的语法格式。下面是一个例子:

  1. //: Mugs.java
  2. // Java 1.1 "Instance Initialization"
  3. class Mug {
  4. Mug(int marker) {
  5. System.out.println("Mug(" + marker + ")");
  6. }
  7. void f(int marker) {
  8. System.out.println("f(" + marker + ")");
  9. }
  10. }
  11. public class Mugs {
  12. Mug c1;
  13. Mug c2;
  14. {
  15. c1 = new Mug(1);
  16. c2 = new Mug(2);
  17. System.out.println("c1 & c2 initialized");
  18. }
  19. Mugs() {
  20. System.out.println("Mugs()");
  21. }
  22. public static void main(String[] args) {
  23. System.out.println("Inside main()");
  24. Mugs x = new Mugs();
  25. }
  26. } ///:~

大家可看到实例初始化从句:

  1. {
  2. c1 = new Mug(1);
  3. c2 = new Mug(2);
  4. System.out.println("c1 & c2 initialized");
  5. }

它看起来与静态初始化从句极其相似,只是static关键字从里面消失了。为支持对“匿名内部类”的初始化(参见第7章),必须采用这一语法格式。