Why is the value of the instance field coming null?

前端 未结 3 928
天命终不由人
天命终不由人 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 19:49

    The object is null because The superclass constructor runs before the subclass constructor,hence it would only be natural that the statement Object obj = null; is executed after calling super class constructor.

    The assignment of Object obj = null; is inlined into the constructor during compile time. This are only accessible in existing instance, and instance does not exist yet when you are in constructor (it is still under construction).

    You can achieve object value(Object = java.lang.Object@3cd1a2f1) by declaringobject object as static.

    static Object obj = null;

    Generally though, it's bad practice to call overriden methods from a constructor in real time.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-23 20:07

    The reason the obj field is null is due to the sequence of steps that occur in the constructor call of Y:

    1. The constructor of Y calls the super constructor which eventually calls readValue of the concrete class Y, therefore assigning a non-null value to the obj field.
    2. After the super constructor finishes, the instance field obj is initialized to null due to the variable initializer:

      Object obj = null;
      

    When you remove the null initializer, it becomes a simple field declaration with no instance initialization to be performed in step 2.

    The apt solution is not to remove the null initializer, but to re-design the whole class hierarchy. For example, since the purpose of readValue seems to just be a setter for a variable, then you don't need to make it override an abstract method in the parent class. Just set it as a separate method and call it after the constructor of Y completes.

    0 讨论(0)
提交回复
热议问题