Java类被加载时执行的顺序问题
2010-01-07 10:55
363 查看
public class Parent
{
//1
static int a = 1;
//2
static
{
a = 10;
System.out.println("parent static code");
}
//4
public Parent()
{
System.out.println("Parent constructor");
System.out.println("Parent a=" + a);
}
public static void main(String[] args)
{
System.out.println("***************");
Parent c = new Child();
}
}
class Child extends Parent
{
static int a = 2;
//3
static
{
a = 20;
System.out.println("child static code");
}
//5
public Child()
{
System.out.println("Child constructor");
System.out.println("Child var a=" + a);
}
}
执行结果:
run:
parent static code
***************
child static code
Parent constructor
Parent a=10
Child constructor
Child var a=20
BUILD SUCCESSFUL (total time: 0 seconds)
Java
语言是动态链接的,只有在需要的时候才去加载java类,在加载java类的时候,首先执行类里面的static代码块,然后进入main入口函数,调用
子类的构造函数,生成子类的对象,子类被加载,调用子类的static代码块,然后开始调用子类的构造函数,调用之前要是检查到父类还没实例化,前去调用
父类的构造函数,保证父类实例化完毕了再去调用子类的构造函数,在子类构造函数中第一句可以用super()调用父类的构造函数感觉像是重新实例化了一个
对象!
现在将程序的入口从父类中转移到子类中,我们再看一下输出的执行流程。。。
class Parent
{
//1
static int a = 1;
//2
static
{
a = 10;
System.out.println("parent static code");
}
//4
public Parent()
{
System.out.println("Parent constructor");
System.out.println("Parent a=" + a);
}
}
public class Child extends Parent
{
static int a = 2;
//3
static
{
a = 20;
System.out.println("child static code");
}
//5
public Child()
{
System.out.println("Child constructor");
System.out.println("Child var a=" + a);
}
public static void main(String[] args)
{
System.out.println("***************");
Parent c = new Child();
}
}
执行结果:
run:
parent static code
child static code
***************
Parent constructor
Parent a=10
Child constructor
Child var a=20
BUILD SUCCESSFUL (total time: 0 seconds)
{
//1
static int a = 1;
//2
static
{
a = 10;
System.out.println("parent static code");
}
//4
public Parent()
{
System.out.println("Parent constructor");
System.out.println("Parent a=" + a);
}
public static void main(String[] args)
{
System.out.println("***************");
Parent c = new Child();
}
}
class Child extends Parent
{
static int a = 2;
//3
static
{
a = 20;
System.out.println("child static code");
}
//5
public Child()
{
System.out.println("Child constructor");
System.out.println("Child var a=" + a);
}
}
执行结果:
run:
parent static code
***************
child static code
Parent constructor
Parent a=10
Child constructor
Child var a=20
BUILD SUCCESSFUL (total time: 0 seconds)
Java
语言是动态链接的,只有在需要的时候才去加载java类,在加载java类的时候,首先执行类里面的static代码块,然后进入main入口函数,调用
子类的构造函数,生成子类的对象,子类被加载,调用子类的static代码块,然后开始调用子类的构造函数,调用之前要是检查到父类还没实例化,前去调用
父类的构造函数,保证父类实例化完毕了再去调用子类的构造函数,在子类构造函数中第一句可以用super()调用父类的构造函数感觉像是重新实例化了一个
对象!
现在将程序的入口从父类中转移到子类中,我们再看一下输出的执行流程。。。
class Parent
{
//1
static int a = 1;
//2
static
{
a = 10;
System.out.println("parent static code");
}
//4
public Parent()
{
System.out.println("Parent constructor");
System.out.println("Parent a=" + a);
}
}
public class Child extends Parent
{
static int a = 2;
//3
static
{
a = 20;
System.out.println("child static code");
}
//5
public Child()
{
System.out.println("Child constructor");
System.out.println("Child var a=" + a);
}
public static void main(String[] args)
{
System.out.println("***************");
Parent c = new Child();
}
}
执行结果:
run:
parent static code
child static code
***************
Parent constructor
Parent a=10
Child constructor
Child var a=20
BUILD SUCCESSFUL (total time: 0 seconds)
相关文章推荐
- Java类被加载时执行的顺序问题
- java 静态变量 静态代码块 加载顺序问题
- 转来的:java中静态块和构造函数的执行顺序问题
- Java中构造函数执行顺序的问题
- Java中for循环中执行顺序问题及break, continue用法
- 【Java】try、catch、finally的执行顺序与返回值问题
- Java中类加载的执行顺序
- java执行时的两个常见问题(无法加载主类)
- Java继承之间类的加载顺序, 以及代码块、构造块的执行顺序
- Java try/catch/finally代码块中加入return后的执行顺序问题
- 通过指令码来判断Java代码的执行顺序(++问题与return和finally的问题)
- java 初始化的加载顺序问题
- 关于Java中静态属性和静态代码块的执行顺序问题
- java中try中return语句与finally块执行顺序问题
- java中子类继承父类程序执行顺序问题
- java静态/动态成员变量、初始化块,父类/子类构造函数执行顺序问题
- 补充《动态加载外部.js文件时候,javascript的执行顺序问题》
- java程序在执行过程中,类,对象以及它们成员加载、初始化的顺序如下:
- Java 类加载执行顺序
- 学习总结:java面试方法重写详解,静态代码块和构造方法执行顺序问题