Why is the value of the instance field coming null?

前端 未结 3 927
天命终不由人
天命终不由人 2021-01-23 19:29

I have this simple piece of code.

abstract class X {
    X() {
        read();
    }

    private void read() {
        Object obj = new Object();
        readVa         


        
3条回答
  •  臣服心动
    2021-01-23 20:07

    This illustrates the dangers of calling an inherited method in a subclass from a superclass constructor. The main danger is that initializers for variables in a subclass run after the superclass constructor completes.

    Here is what happens.

    1. An object of y is created.
    2. The superclass constructor X() is called, which calls read().
    3. The read method creates a new Object and passes it to readValue, which is implemented in Y.
    4. The readValue method in Y sets obj to the new object.
    5. The superclass constructor X() completes, and initializers run now in Y, setting obj to null.
    6. The printer method prints "Object = null".

    If you remove the declaration of obj in Y, then there is no initializer to run, and the obj variable retains its value.

    The JLS, Section 12.5, states:

    [A]ll the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§4.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.

    (emphasis mine)

    and

    Unlike C++, the Java programming language does not specify altered rules for method dispatch during the creation of a new class instance. If methods are invoked that are overridden in subclasses in the object being initialized, then these overriding methods are used, even before the new object is completely initialized.

提交回复
热议问题