加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在实例化领域和实例化构造函数有什么区别?

发布时间:2020-12-14 05:23:28 所属栏目:Java 来源:网络整理
导读:这样做有什么区别? public class SomeClass { SomeObject obj = new SomeObject(); //rest of the code} 和这个 public class SomeClass { SomeObject obj; public SomeClass(){ obj = new SomeObject(); } //rest of the code} 解决方法 根据Java语言规范
这样做有什么区别?
public class SomeClass {
    SomeObject obj = new SomeObject();
    //rest of the code
}

和这个

public class SomeClass {
    SomeObject obj;
    public SomeClass(){
       obj = new SomeObject();
    }
    //rest of the code
}

解决方法

根据Java语言规范第 12.5 Creation of New Class Instances章:

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

所以区别只是一步(步骤4.或步骤5.),但结果是一样的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读