java对象初始化问题--阿里🐮刨析

[toc]

一. why引入

因为java对象初始化问题是比较基础的java知识点,同时,也是众多面试中百问不厌的问题,所以,我们通过阿里工程师的例子和刨析来好好理解他。

二. 引发问题

会触发Java object initialization order 问题,这种问题比较少见。

三. 举例

package com.xn.web.budget;

public class Test2 extends Test {
    public int a = 100;

    public Test2() {
        super();
        System.out.println(a);
        a = 200;
    }

    public static void main(String[] args){
        System.out.println(new Test2().a);
    }
}

package com.xn.web.budget;

public class Test {
    public Test() {
        System.out.println(((Test2)this).a);
    }
}

结果输出:

0
100
200

解析对象的初始化顺序:

  1. 为A类分配内存空间,初始化所有成员变量为默认值,包括primitive类型(int=0,boolean=false,…)和Reference类型。
  2. 调用A类构造函数。
  3. 调用B类构造函数。
  4. 调用Object空构造函数。(java编译器会默认加此构造函数,且object构造函数是个空函数,所以立即返回)
  5. 初始化B类成员变量,因为B类没有成员变量,跳过。
  6. 执行sysout输出子类A的成员变量小a。// 此时为0
  7. 初始化A类成员变量,将A类成员变量小a赋值100。
  8. 执行sysout输出当前A类的成员变量小a。// 此时为100
  9. 赋值当前A类的成员变量小a为200。
  10. main函数中执行sysout,输出A类实例的成员变量小a。// 此时为200

加粗的那两行描述是重点,结论是成员变量初始化是在父类构造函数调用完后,在此之前,成员变量的值均是默认值

其实这类问题,熟悉原理是一方面,本质上只要不在构造函数中插入过多的业务逻辑,出问题的概率也会低很多。

最后,我们再来看看JLS中给出的Java类对象初始化顺序定义,这是一个带条件分支的流程描述:

  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 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. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
  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.

引用自:传送


   转载规则


《java对象初始化问题--阿里🐮刨析》 Will 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
编辑器IDE--vim 编辑器IDE--vim
[toc] 一. Emacs or vim?素有的两大编辑器之争vim、emacs, 我们也就不再多说了,各具千秋,都值得我们学习,或许融入到他们之中,才能体会到他们的独特。说融入他们是容易, 可为什么平时我们还是很少的使用他们呢? 这可
2018-05-02
下一篇 
git养成日记 git养成日记
[toc] 前言基础入门,参考廖雪峰老师的git教程 一. git日常操作 git add fileName;git commit -m ‘提交说明’;git push -u;//提交到githubgit status -s//查看文件状
2018-04-26
  目录