gpt4 book ai didi

java - 大括号下的语句是否首先加载?

转载 作者:行者123 更新时间:2023-12-02 23:54:12 25 4
gpt4 key购买 nike

我知道静态内容首先加载到内存中,但是当我没有提到它是静态时,为什么在“CT”之前打印“IT”?

class Person
{
Person()
{
System.out.print(" CP");
}

static
{
System.out.print("SP");
}
}

class Teacher extends Person
{
Teacher()
{
System.out.print(" CT");
}

{
System.out.print(" IT");
}
}


public class StaticTest
{
public static void main(String[] args)
{
Person p = new Teacher();
}
}

最佳答案

诸如{System.out.print("IT");}之类的初始化程序 block 在构造函数之前执行。实际上,它们被复制到每个构造函数的开头。

Initializing Instance Members

Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods.

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

{
// whatever code is needed for initialization goes here
}

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

( Source )

更准确地说,这是 JLS 中描述的初始化顺序。 :

  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.

请注意,实例初始值设定项在步骤 4 中执行,先于构造函数主体(步骤 5)。

关于java - 大括号下的语句是否首先加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com