IT Notes‎ > ‎Java‎ > ‎Java Language‎ > ‎《Java 解惑》笔记‎ > ‎

Puzzle 40: The Reluctant Constructor

实例初始化操作是先于构造器的程序体而运行的。

public class Reluctant {
        private Reluctant internalInstance = new Reluctant(); //tag1

        public Reluctant() throws Exception { //tag2
               throw new Exception("I'm not coming out");
       }

        public static void main(String[] args) {
               try {
                     Reluctant b = new Reluctant(); //tag3
                     System. out.println("Surprise!" );
              } catch (Exception ex) {
                     System. out.println("I told you so" );
              }
       }
}

这里,按照 JLS 12.5 的说法:

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

如上,域的实例化(第4步)将在构造器执行(第5步)之前。如此在实例化类时,会出现:
  1. 实例化域,包含一个构造器,因此要再实例化类,进入第2步,
  2. 实例化类,先实例化域,进入第1步.
如此递归调用绵绵无绝期,最后抛出 StackOverflowError 的异常,这个异常不是 Exception 类型,catch 语句是不能捕获的。

参:



Comments