Java子类构造的执行顺序

1.package test;   
2.
3.public class Test {
4. public static void main(String[] args) {
5. new Son();
6. }
7.
8. public static void print(String msg) {
9. System.out.println(msg);
10. }
11.
12. public static int getInt(String name) {
13. print("static member " + name);
14. return 0;
15. }
16.
17. public static String getString(String name) {
18. print("nonstatic member " + name);
19. return "";
20. }
21.
22. static class Parent {
23. Parent() {
24. print("constructor parent");
25. }
26.
27. static {
28. print("static block parent");
29. }
30.
31. {
32. print("nonstatic block parent");
33. }
34.
35. String str = getString("parent");
36.
37. static int i = getInt("parent");
38. }
39.
40. static class Son extends Parent {
41. Son() {
42. print("constructor son");
43. }
44.
45. static {
46. print("static block son");
47. }
48.
49. {
50. print("nonstatic block son");
51. }
52.
53. String str = getString("son");
54.
55. static int i = getInt("son");
56. }
57.}



这个说明了Java类的执行顺序为:
父类的静态块--->父类的静态成员--->子类的静态块--->子类的静态成员--->父类的非静态块--->父类的非静态成员--->

父类的构造函数--->子类的非静态块--->子类的非静态成员--->子类的构造函数

内部类:先初始化内部类,后外部类

构造器:先初始化父类的构造器,后初始化子类构造器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值