前两天去面试,面试官问类成员的初始化顺序,比如静态块、构造函数、成员变量这些,然后再有父类、子类... 如此这般,我是脑子里快浆糊了,看来基本功不扎实啊,所以我决定仔细研究一下,再写出来与大家分享一下。
1、普通成员变量与构造函数
public class Example {
int number = 1;
public Example() {
number = 2;
}
}
执行:
Example example = new Example();
Log.d("TAG", "number = " + example.number);
执行结果:number = 2
结果为2,表明构造函数是在之后才执行的
顺序从高到低为 :1、普通成员变量 2、构造函数
2、普通成员变量、构造函数与静态成员变量
public class Base {
public Base(String s) {
Log.d("TAG", s);
}
}public class Example {
Base base1 = new Base("common member variable");
static Base base2 = new Base("static member variable");
public Example() {
Log.d("TAG", " Example constructor");
}
}
执行:
Example example = new Example();
执行结果:
static member variable
common member variable
common member variable
Example constructor
顺序从高到低为 :1、静态成员变量 2、 普通成员变量 3、构造函数
3、普通成员变量、构造函数、静态成员变量与静态初始化块
public class Example {
Base base1 = new Base("common member variable");
static Base base2 = new Base("static member variable");
public Example() {
Log.d("TAG", "Example constructor");
}
static {
Log.d("TAG", "static module");
}
执行:
Example example = new Example();
执行结果:
static member variable
static module
common member variable
common member variable
Example constructor
public class Example {
Base base1 = new Base("common member variable");
public Example() {
Log.d("TAG", "Example constructor");
}
static {
Log.d("TAG", "static module");
}
static Base base2 = new Base("static member variable");
}
执行结果:
static module
static member variable
common member variable
Example constructor
可以看出来,静态变量与静态模块的初始化顺序与它们的位置有关
所以,顺序从高到低为 :1、静态成员变量或 静态初始化模块 2、 普通成员变量 3、构造函数
4、普通成员变量、构造函数、静态成员变量、静态初始化块与非静态模块
public class Example {
Base base1 = new Base("common member variable");
public Example() {
Log.d("TAG", "Example constructor");
}
{
Log.d("TAG", "common module");
}
static {
Log.d("TAG", "static module");
}
static Base base2 = new Base("static member variable");
}
执行结果:
static module
static member variable
common member variable
common module
Example constructor
public class Example {
public Example() {
Log.d("TAG", "Example constructor");
}
{
Log.d("TAG", "common module");
}
Base base1 = new Base("common member variable");
static {
Log.d("TAG", "static module");
}
static Base base2 = new Base("static member variable");
}
执行结果:
static module
static member variable
common module
common member variable
Example constructor
看来果然是这样的。
顺序从高到低为 :1、静态成员变量或 静态初始化模块 2、 普通成员变量或非静态模块 3、构造函数
5、继承关系中的初始化情况
public class Parent {
public Parent() {
Log.d("TAG", "Parent constructor");
}
{
Log.d("TAG", "Parent common module");
}
Base base1 = new Base("Parent common member variable");
static {
Log.d("TAG", "Parent static module");
}
static Base base2 = new Base("Parent static member variable");
}public class Child extends Parent {
public Child() {
Log.d("TAG", "Child constructor");
}
{
Log.d("TAG", "Child common module");
}
Base base1 = new Base("Child common member variable");
static {
Log.d("TAG", "Child static module");
}
static Base base2 = new Base("Child static member variable");
}
执行
Child child = new Child();执行结果为:
Parent static module
Parent static member variable
Parent static member variable
Child static module
Child static member variable
Parent common module
Parent common member variable
Parent constructor
Child common module
Child common member variable
Child constructor
Child static member variable
Parent common module
Parent common member variable
Parent constructor
Child common module
Child common member variable
Child constructor
本文详细探讨了Java中类成员的初始化顺序,包括静态成员、普通成员、构造函数及继承关系中的初始化流程,并通过实例代码验证了不同类型的成员变量及初始化块的执行顺序。

1578

被折叠的 条评论
为什么被折叠?



