现在的位置: 首页 > 综合 > 正文

在Java中自由块的执行顺序

2012年03月03日 ⁄ 综合 ⁄ 共 2552字 ⁄ 字号 评论关闭

  • java中的自由块分为静态的自由块和非静态的自由块。
  • 非静态自由块的执行时间是:在执行构造函数之前。
  • 静态自由块的执行时间是:class文件加载时执行。
  • 非静态自由块可以多次执行,只要初始化一个对象就会执行,但是静态自由块只会在类装载的时候执行一次,一般用来初始化类的静态变量的值。
  • 每次初始化一个对象,都会导致一次非静态块的执行。
  • 如果涉及到继承,则是:首先执行父类的非静态块,然后是父类的构造函数,接着是自己的自由块,最后是自己的构造函数。
  • 静态块的执行时机是在class文件装载的时候,class文件只会装载一次,因此静态块只会执行一次,后面再使用这个类时,不会再执行静态块。
  • 静态块的执行时机是在class装载后的初始化阶段。如果采用ClassLoader的loadclass来仅仅装 载类而不初始化,是不会触发静态块的执行的。采用Class的forname(String)是采用了默认的initialize为true的情况,也就 是初始化了。如果使用forname(String name,boolean initialize, ClassLoader loader),设置initialize为false,则不会执行静态块。
  • 在执行class装载后的初始化阶段包括:运行<clinit>方法,这个方法中就是类变量的初始化语句和静态自由块语句。这个方法是由java的编译器收集信息后生成的,不能显示的调用。

下面通过例子来说明:

父类

复制代码 代码如下:
father.java

public class father {

static{//静态块
System.out.println("father'sSTATIC free block running");
}

{//非静态块
System.out.println("father'sfree block running");
}

public father(){

System.out.println("father'sconstructor running");

}
}

子类

复制代码 代码如下:
son.java
public class son extends father{
static{//静态块
System.out.println("son'sSTATIC free block running");
}

{//非静态块
System.out.println("son's freeblock running");
}

public son() {
// TODO Auto-generated constructor stub
System.out.println("son'sconstructor running");
}
}

主函数所在类

复制代码 代码如下:
test.java

public class test{

public static void main(String[] args) {

Class f;

try {

System.out.println("--------beforeload father--------");

f=Class.forName("freeblock.father");

System.out.println("--------afterload father---------");

System.out.println("--------beforeinitial father object--------");

f.newInstance();

System.out.println("--------afterinitial father object--------");

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (InstantiationException e) {
e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();
}
Class s;

try {

System.out.println("-------beforeload son--------");

s=Class.forName("freeblock.son");

System.out.println("--------afterload son-------");

System.out.println("--------beforeinitial son object----------");

s.newInstance();

System.out.println("--------afterinitial son object-----------");

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

执行结果:

--------before loadfather--------

father's STATIC free blockrunning

--------after loadfather---------

--------before initial fatherobject--------

father's free block running

father's constructor running

--------after initial fatherobject--------

-------before load son--------

son's STATIC free block running

--------after load son-------

--------before initial sonobject----------

father's free block running

father's constructor running

son's free block running

son's constructor running

--------after initial son object-----------

抱歉!评论已关闭.