编程语言
首页 > 编程语言> > java-为什么实例字段的值变为空?

java-为什么实例字段的值变为空?

作者:互联网

我有这段简单的代码.

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

    private void read() {
        Object obj = new Object();
        readValue(obj);
    }
    protected abstract void readValue(Object obj);
}

class Y extends X {

    Object obj = null;
    Y() {
        super();
    }

    @Override
    protected void readValue(Object obj) {
        this.obj = obj;
    }

    void printer() {
        System.out.println("Object = " + obj);
    }
}

class Runner {
    public static void main(String[] args) {
        Y y = new Y();
        y.printer();
    }
}

当我运行上面的代码时,该对象被打印为null. (我得到“ Object = null”)
令人惊讶的是,在类Y中,我删除了null声明

Object obj;

打印对象的实际值.
类似的东西(“对象= java.lang.Object@3cd1a2f1”)
为什么会观察到这种行为? “这个”指的是什么?如果仅声明对象,则将其初始化为null,那么为什么会有这种异常行为呢?

解决方法:

这说明了从超类构造函数的子类中调用继承方法的危险.主要危险在于,超类构造函数完成后,子类中变量的初始化程序就会运行.

这是发生了什么.

>创建了一个y对象.
>调用超类构造函数X(),该构造函数调用read().
> read方法创建一个新的Object并将其传递给readValue,该实现在Y中实现.
> Y中的readValue方法将obj设置为新对象.
>超类构造函数X()完成,并且初始化程序现在在Y中运行,将obj设置为null.
>打印机方法将打印“ Object = null”.

如果删除Y中的obj声明,则没有要运行的初始化程序,并且obj变量保留其值.

JLS, Section 12.5指出:

[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.

(强调我的)

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.

标签:java,constructor,inheritance,abstract-class,overriding
来源: https://codeday.me/bug/20191014/1912108.html