java父类子类初始化顺序

java父类子类初始化顺序

Cannot reference 'FlightScheduleApp.sources' before supertype constructor has been called引发的思考

问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class BaseApp<S,E> {
private final List<S> sources;
private final List<Location> locations;
private final List<E> entries;

public BaseApp(List<S> sources, List<Location> locations, List<E> entries) {
this.sources = sources;
this.locations = locations;
this.entries = entries;
}
}
public class FlightScheduleApp extends BaseApp<Plane, FlightEntry> {
private final List<Plane> sources=new ArrayList<>();
private final List<Location> locations=new ArrayList<>();
private final List<FlightEntry> entries=new ArrayList<>();

public FlightScheduleApp() {
//Cannot reference 'FlightScheduleApp.sources' before supertype constructor has been called
super(sources,locations,entries);
}
}

意思是在父类构函数初始化之前不能引用这个变量。

解决办法

在子类这个变量前加上startic修饰符时,就不再报错。

猜想验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 父类
*/
public class Base {

static String sVar = getString("父类静态变量初始化");
public String var = getString("父类非静态变量初始化");

public Base() {
System.out.println("父类构造函数 start");
draw("父类调用draw方法");//会调用子类覆盖后的方法,这里是null
System.out.println("父类构造函数 end");
}

static String getString(String base) {
System.out.println(base);
return base;
}

public void draw(String string) {
System.out.println(string);
}
}

/**
* 子类
*/
public class SubClass extends Base {

public String var = getString("子类非静态变量初始化");
private String subVar = getString("子类私有变量初始化");
static String superVar = getString("子类静态变量初始化");

SubClass() {
System.out.println("子类构造函数start");
draw("子类调用draw方法");
System.out.println("子类构造函数end");
}

public void draw(String string) {
System.out.println(string + subVar);
}

public static void main(String[] args) {
new SubClass();
}
}

结果

1
2
3
4
5
6
7
8
9
10
11
父类静态变量初始化
子类静态变量初始化 <-----
父类非静态变量初始化 <-----
父类构造函数 start
父类调用draw方法null
父类构造函数 end
子类非静态变量初始化 <-----
子类私有变量初始化
子类构造函数start
子类调用draw方法子类初始化私有变量
子类构造函数end

可以发现,父类构造函数初始化早于子类非静态变量的初始化,晚于子类静态变量的初始化,所以当我们需要把变量改为static

#
You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×