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类的执行顺序为:
父类的静态块--->父类的静态成员--->子类的静态块--->子类的静态成员--->父类的非静态块--->父类的非静态成员--->
父类的构造函数--->子类的非静态块--->子类的非静态成员--->子类的构造函数
内部类:先初始化内部类,后外部类
构造器:先初始化父类的构造器,后初始化子类构造器