简体   繁体   English

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

[英]Are statements under curly braces load first?

I know static contents are loaded first in the memory, but why is 'IT' printed before 'CT' when I haven't mentioned it as static? 我知道首先将静态内容加载到内存中,但是为什么当我没有提到它为静态时,为什么在“ 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();
    }
}

Initializer blocks such as {System.out.print(" IT");} are executed before the constructor. 初始化程序块,例如{System.out.print(" IT");} ,在构造函数之前执行。 Actually, they are copied to the beginning of each constructor. 实际上,它们被复制到每个构造函数的开头。

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. 使用构造函数初始化实例变量有两种选择: 初始化块和final方法。

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword : 实例变量的初始化程序块看起来像静态初始化程序块,但没有static关键字

{ {
// whatever code is needed for initialization goes here //初始化所需的任何代码都在这里
} }

The Java compiler copies initializer blocks into every constructor. Java编译器将初始化程序块复制到每个构造函数中。 Therefore, this approach can be used to share a block of code between multiple constructors. 因此,该方法可用于在多个构造函数之间共享代码块。

( Source ) 来源

And to be more exact, here's the initialization order as described in the JLS : 更确切地说,这是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. 如果此构造函数以同一个类中的另一个构造函数的显式构造函数调用(第8.8.7节)开头(使用此方法),则使用这五个步骤评估参数并递归处理该构造函数调用。 If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; 如果该构造函数调用突然完成,则该过程由于相同的原因突然完成; otherwise, continue with step 5. 否则,请继续执行步骤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). 如果此构造函数用于Object以外的其他类,则此构造函数将以显式或隐式调用超类构造函数(使用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。

  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。

  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. 否则,此过程将正常完成。

Notice that the instance initializers are executed in step 4, prior to the body of the constructor (step 5). 请注意,实例构造函数在构造函数的主体(步骤5)之前在步骤4中执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM