ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-14 03:02:55  阅读:363  来源: 互联网

标签:java constructor inheritance abstract-class overriding


我有这段简单的代码.

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有