创建一个类
C的
对象:
1. 加载所有得类。
2. 执行静态初始化。
3. 分配内存,并初始化为零。
4. 处理调用的C的构造函数的参数。
6. 处理隐式或者显式的构造函数( this()或者super())。
7. 初始化C中的变量。
8. 执行C的“instance initialization”。
9. 执行
调用的C的构造函数。
class
Base{
static
{
System.
out
.println(
"Begin to initialize static block of Base!"
);
}
int
basicChoice
=100;
{
System.
out
.println(
"I am instance initialization of Base!"
);
System.
out
.println(
"In Base instance initialization,Base::basicChoice="
+
basicChoice
);
System.
out
.println(
" "
);
}
Base(){
this
(
checkValue
(240));
System.
out
.println(
"In Base constructor Base(int choice), Base::basicChoice="
+
basicChoice
);
basicChoice
= 240;
System.
out
.println(
"In Base constructor Base(int choice), Base::basicChoice="
+
basicChoice
);
System.
out
.println(
" "
);
}
private
Base(Void check){
System.
out
.println(
"In Base constructor Base(Void check)"
);
}
private
static
Void checkValue(
int
choice){
if
(choice<0){
throw
new
IllegalArgumentException(
"Choice must be positive"
);
}
else
{
System.
out
.println(
"Choice is right!"
);
System.
out
.println(
" "
);
}
return
null
;
}
}
public
class
ConstructorOrder
extends
Base {
int
outconstructor_length
=
basicChoice
;
String
name
;
static
{
System.
out
.println(
"Begin to initialize static block of ConstructorOrder!"
);
System.
out
.println(
" "
);
}
{
System.
out
.println(
"I am instance initialization of ConstructorOrder !"
);
System.
out
.println(
" "
);
}
public
ConstructorOrder(){
this
(
check
());
System.
out
.println(
"In ConstructorOrder constructor ConstructorOrder(),outconstructor_length="
+
outconstructor_length
+
";"
+
"name="
+
name
);
name
=
new
String(
"NianNian"
);
outconstructor_length
=10;
System.
out
.println(
"In ConstructorOrder constructor ConstructorOrder(),outconstructor_length="
+
outconstructor_length
+
";"
+
"name="
+
name
);
}
public
ConstructorOrder(
boolean
result){
if
(result){
System.
out
.println(
"In ConstructorOrder constructor ConstructorOrder(boolean result), paramter is true"
);
}
else
{
System.
out
.println(
"In ConstructorOrder constructor ConstructorOrder(boolean result), paramter is false"
);
}
}
private
static
boolean
check(){
System.
out
.println(
"Check return true!"
);
return
true
;
}
public
static
void
main(String [] args){
ConstructorOrder
obj
=
new
ConstructorOrder();
}
}
执行结果是:
Begin to initialize static block of Base!
Begin to initialize static block of ConstructorOrder!
Check return true!
Choice is right!
I am instance initialization of Base!
In Base instance initialization,Base::basicChoice=100
In Base constructor Base(Void check)
In Base constructor Base(int choice), Base::basicChoice=100
In Base constructor Base(int choice), Base::basicChoice=240
I am instance initialization of ConstructorOrder !
In ConstructorOrder constructor ConstructorOrder(boolean result), paramter is true
In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=240;name=null
In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=10;name=NianNian
对执行结果的解释:
1. 执行 “ConstructorOrder obj = new ConstructorOrder()". 这触发ConstructorOrder.class的调用,因为ConstructorOrder继承Base,所以接着调用Base.class.
2. 执行Base类中的静态初始化:
static{
System.out.println("Begin to initialize static block of Base!");
}
3. 执行Constructor类中的静态初始化:
static{
System.out.println("Begin to initialize static block of ConstructorOrder!");
System.out.println(" ");
}
4. 继续处理”ConstructorOrder obj = new ConstructorOrder()"中调用构造函数的传递参数,调用的是无参数构造函数,不需要特别处理。
5. 开始执行ConstructorOrder(),
5.1首先执行显式或者隐式调用的this或者super。在这个程序中是显式的调用"this(check())"。就是执行ConstructorOrder(check())。
6.执行ConstructorOrder(check())
6.1 首先处理参数check(),就是执行函数check(),输出:“Check return true!”。
6.2 接着处理构造函数“ConstructorOrder(boolean result)”中隐式调用的super()。
7.开始执行构造函数Base()
7.1 处理this(checkValue(240));也就是处理Base(checkValue(240))。
8.处理Base(checkValue(240))
8.1 首先处理参数,就是执行函数checkvalue(240),输出:“Choice is right!"
8.2 处理隐式的Object().
9.Object对象构造完,初始化Base类中的变量。
int basicChoice=100;
10.执行Base中的"Instance initialization"。输出:
I am instance initialization of Base!
In Base instance initialization,Base::basicChoice=100
11.接着继续执行步骤8,就是Base(Void)剩下的部分。输出:In Base constructor Base(Void check)
12. 接着继续执行步骤7,就是执行Base()剩下的部分。
12.1输出:In Base constructor Base(int choice), Base::basicChoice=100
12.2执行”basicChoice = 240“
12.3接着输出:In Base constructor Base(int choice), Base::basicChoice=240
13. 初始化完Base类后,初始化ConstructorOrder类中的变量。
int outconstructor_length=basicChoice;
String name;
14. 接着执行ConstructorOrder中的"Instance initialization"。输出:I am instance initialization of ConstructorOrder !
15. 接着继续执行步骤6,就是ConstructorOrder(boolean)剩下部分。输出:
In ConstructorOrder constructor ConstructorOrder(boolean result), paramter is false
16. 接着继续执行步骤5,就是ConstructorOrder()剩下部分。
16.1输出:In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=240;name=null
16.2执行 “name = new String("NianNian");”
16.3执行 “outconstructor_length=10;”
16.4输出:In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=10;name=NianNian
17. obj对象创建完毕。
Begin to initialize static block of Base!
Begin to initialize static block of ConstructorOrder!
Check return true!
Choice is right!
I am instance initialization of Base!
In Base instance initialization,Base::basicChoice=100
In Base constructor Base(Void check)
In Base constructor Base(int choice), Base::basicChoice=100
In Base constructor Base(int choice), Base::basicChoice=240
I am instance initialization of ConstructorOrder !
In ConstructorOrder constructor ConstructorOrder(boolean result), paramter is true
In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=240;name=null
In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=10;name=NianNian
对执行结果的解释:
1. 执行 “ConstructorOrder obj = new ConstructorOrder()". 这触发ConstructorOrder.class的调用,因为ConstructorOrder继承Base,所以接着调用Base.class.
2. 执行Base类中的静态初始化:
static{
System.out.println("Begin to initialize static block of Base!");
}
3. 执行Constructor类中的静态初始化:
static{
System.out.println("Begin to initialize static block of ConstructorOrder!");
System.out.println(" ");
}
4. 继续处理”ConstructorOrder obj = new ConstructorOrder()"中调用构造函数的传递参数,调用的是无参数构造函数,不需要特别处理。
5. 开始执行ConstructorOrder(),
5.1首先执行显式或者隐式调用的this或者super。在这个程序中是显式的调用"this(check())"。就是执行ConstructorOrder(check())。
6.执行ConstructorOrder(check())
6.1 首先处理参数check(),就是执行函数check(),输出:“Check return true!”。
6.2 接着处理构造函数“ConstructorOrder(boolean result)”中隐式调用的super()。
7.开始执行构造函数Base()
7.1 处理this(checkValue(240));也就是处理Base(checkValue(240))。
8.处理Base(checkValue(240))
8.1 首先处理参数,就是执行函数checkvalue(240),输出:“Choice is right!"
8.2 处理隐式的Object().
9.Object对象构造完,初始化Base类中的变量。
int basicChoice=100;
10.执行Base中的"Instance initialization"。输出:
I am instance initialization of Base!
In Base instance initialization,Base::basicChoice=100
11.接着继续执行步骤8,就是Base(Void)剩下的部分。输出:In Base constructor Base(Void check)
12. 接着继续执行步骤7,就是执行Base()剩下的部分。
12.1输出:In Base constructor Base(int choice), Base::basicChoice=100
12.2执行”basicChoice = 240“
12.3接着输出:In Base constructor Base(int choice), Base::basicChoice=240
13. 初始化完Base类后,初始化ConstructorOrder类中的变量。
int outconstructor_length=basicChoice;
String name;
14. 接着执行ConstructorOrder中的"Instance initialization"。输出:I am instance initialization of ConstructorOrder !
15. 接着继续执行步骤6,就是ConstructorOrder(boolean)剩下部分。输出:
In ConstructorOrder constructor ConstructorOrder(boolean result), paramter is false
16. 接着继续执行步骤5,就是ConstructorOrder()剩下部分。
16.1输出:In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=240;name=null
16.2执行 “name = new String("NianNian");”
16.3执行 “outconstructor_length=10;”
16.4输出:In ConstructorOrder constructor ConstructorOrder(),outconstructor_length=10;name=NianNian
17. obj对象创建完毕。