[英]Methods step by step
I have kind of code.我有一种代码。 And I want to know why "block b" running first, but not "constructorb".
我想知道为什么“block b”首先运行,而不是“constructorb”。 The same thing at Class A. Thanks.
Class A 的相同内容。谢谢。
class B {
B() {
System.out.println("constructorb");
}
{
System.out.println("block b");
}
}
class A extends B {
A() {
super();
System.out.println("constructor a");
}
{
System.out.println("block a");
}
public static void main(String[] args) {
A a = new A();
}
}
output:
block b
constructorb
block a
constructor a
Instance initializers (ie the {}
blocks) are effecitvely, if not literally, folded into the constructor.实例初始值设定项(即
{}
块)有效地(如果不是字面意思)折叠到构造函数中。 The same thing happens with field initialization.字段初始化也会发生同样的事情。 Both instance initializers and field initializations are inserted just after the
super(...)
constructor call (whether implicit or explicit) but before the rest of the constructor code.实例初始化程序和字段初始化都插入到
super(...)
构造函数调用之后(无论是隐式还是显式),但在构造函数代码的 rest 之前。 So when you have:所以当你有:
public class Foo {
private int bar = 5;
{
System.out.println("Instance initializer");
}
public Foo() {
System.out.println("Constructor");
}
}
It's compiled as if the following was written:它的编译就像以下内容一样:
public class Foo {
private int bar;
public Foo() {
super();
this.bar = 5;
System.out.println("Instance initializer");
System.out.println("Constructor");
}
}
The order that fields are initialized and instance initializers are executed is, however, determined by their order in the source code.但是,初始化字段和执行实例初始化程序的顺序取决于它们在源代码中的顺序。 But note that the placement of the constructor is irrelevant.
但请注意,构造函数的位置无关紧要。
This is described in §8.8.7.1 of the Java Language Specification .这在Java 语言规范的 §8.8.7.1 中有描述。
It is because block a
is in a non-static block.这是因为
block a
在非静态块中。 A non-static block executes when the object is created, before the constructor在创建 object 时,在构造函数之前执行非静态块
It is a normal Object instance initialization process.这是一个正常的 Object 实例初始化过程。
There are two principles in the process of instance initialization:实例初始化的过程有两个原则:
static method
would run before constructor method
. static method
将在constructor method
之前运行。parent class
would run before child class
. parent class
将在child class
之前运行。 Now, you have parent class
named B
, and child class
named A
extends B. So, the class B
should run before class A
.现在,您有名为
B
的parent class
和名为A
child class
扩展了 B。因此, class B
应该在A
之前运行
Next.下一个。 In class
B
, you define a static method
which is在 class
B
中,您定义了一个static method
,即
{
System.out.println("block b");
}
So, the string block b
is printed first.因此,首先打印字符串
block b
。 And then constructor method runs:然后构造方法运行:
B() {
System.out.println("constructorb");
}
So, the string constructorb
is printed second.所以,字符串
constructorb
是第二个打印出来的。
Now, class B
runs finish.现在,class
B
运行完成。 And then class A
runs as the order static mehod
first and constructor method
second.然后 class
A
首先按照static mehod
方法运行,其次是constructor method
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.