Java中类的初始化顺序
2017-03-08 12:48
197 查看
先写代码分析,以后再补文章内容
父类:
package cn.tom.init;
/**
* Created by lenovo on 2017/3/8.
*/
public class ParentInit {
private static String parentStr = "我是静态字符串parentStr";
private String parentStr2 = "我是字符串parentStr";
public ParentInit() {
System.out.println("我是构造器ParentInit" + "---------" + 7);
}
static {
System.out.println("静态代码块中===" + parentStr + "----------" + 1);
System.out.println("我是静态代码块ParentInit" + "---------" + 2);
}
{
System.out.println(parentStr2 + "-----------" + 5);
System.out.println("我是代码块ParentInit" + "----------" + 6);
}
static void get() {
System.out.println("我是静态方法ParentInit" + "-----------" + 11);
}
void query() {
System.out.println("我是方法ParentInit");
}
}
子类:
package cn.tom.init;
/**
* Created by lenovo on 2017/3/8.
* 类的初始化顺序
*/
public class ChildInit extends ParentInit {
private static String childStr = "我是静态字符串childStr";
private String childStr2 = "我是字符串childStr";
public ChildInit() {
System.out.println("我是构造器ChildInit" + "----------" + 10);
}
static {
System.out.println("静态代码块" + childStr + "---------" + 3);
System.out.println("我是静态代码块ChildInit" + "----------" + 4);
}
{
System.out.println("代码块中" + childStr2 + "----------" + 8);
System.out.println("我是代码块ChildInit" + "-----------" + 9);
}
static void get() {
System.out.println("我是方法ChildInit");
}
@Override
void query() {
System.out.println("我是方法ChildInit" + "--------" + 12);
}
public static void main(String[] args) {
ParentInit parentInit = new ChildInit();
parentInit.get();//此处调用的是父类的,静态方法不能重写
parentInit.query();//此处调用时子类的
}
}
运行效果:
由以上得出结论:
1、父类的【父类静态变量】>【父类静态代码块】>【子类静态变量】>【子类静态代码块】>【父类成员变量】>【父类代码块】>【父类构造器】>【子类成员变量】>【子类代码块】>【子类构造器】
2、子类不能重写父类的静态方法
父类:
package cn.tom.init;
/**
* Created by lenovo on 2017/3/8.
*/
public class ParentInit {
private static String parentStr = "我是静态字符串parentStr";
private String parentStr2 = "我是字符串parentStr";
public ParentInit() {
System.out.println("我是构造器ParentInit" + "---------" + 7);
}
static {
System.out.println("静态代码块中===" + parentStr + "----------" + 1);
System.out.println("我是静态代码块ParentInit" + "---------" + 2);
}
{
System.out.println(parentStr2 + "-----------" + 5);
System.out.println("我是代码块ParentInit" + "----------" + 6);
}
static void get() {
System.out.println("我是静态方法ParentInit" + "-----------" + 11);
}
void query() {
System.out.println("我是方法ParentInit");
}
}
子类:
package cn.tom.init;
/**
* Created by lenovo on 2017/3/8.
* 类的初始化顺序
*/
public class ChildInit extends ParentInit {
private static String childStr = "我是静态字符串childStr";
private String childStr2 = "我是字符串childStr";
public ChildInit() {
System.out.println("我是构造器ChildInit" + "----------" + 10);
}
static {
System.out.println("静态代码块" + childStr + "---------" + 3);
System.out.println("我是静态代码块ChildInit" + "----------" + 4);
}
{
System.out.println("代码块中" + childStr2 + "----------" + 8);
System.out.println("我是代码块ChildInit" + "-----------" + 9);
}
static void get() {
System.out.println("我是方法ChildInit");
}
@Override
void query() {
System.out.println("我是方法ChildInit" + "--------" + 12);
}
public static void main(String[] args) {
ParentInit parentInit = new ChildInit();
parentInit.get();//此处调用的是父类的,静态方法不能重写
parentInit.query();//此处调用时子类的
}
}
运行效果:
由以上得出结论:
1、父类的【父类静态变量】>【父类静态代码块】>【子类静态变量】>【子类静态代码块】>【父类成员变量】>【父类代码块】>【父类构造器】>【子类成员变量】>【子类代码块】>【子类构造器】
2、子类不能重写父类的静态方法
相关文章推荐
- Java:深度理解JAVA本身的构造器及子父类构造方法的初始化顺序
- Java中类初始化的顺序
- JAVA笔试之初始化顺序
- java 初始化顺序
- JAVA类初始化顺序,经典例程
- Java初始化顺序
- [转贴] Java程序初始化顺序
- [java]初始化顺序
- Java中静态变量和非静态变量的初始化顺序
- JAVA面试题解惑系列(一)――类的初始化顺序
- Java中类初始化的顺序
- java中初始化的顺序
- 初始化顺序(Thinking in Java中的一段有意思的例子)
- java对象初始化顺序
- Java程序初始化顺序
- Java中类初始化的顺序
- Java中类初始化的顺序
- JAVA初始化顺序
- java学习笔记3:对象的初始化顺序
- JAVA面试题解惑系列(一)——类的初始化顺序