[#0x0008] class loading: an example

  class loading是一个比较复杂的过程。一般说来,类是在其static member被访问时被加载的。在加载时会做的一件事是:初始化static member和static代码段(static block, i.e. static {......}),当然,static是只会被执行only once的。

  以下是一个例子(adapted from Chapter 7, Thinking in Java, Fourth Edition):

//@file Beetle.java

import static java.lang.System.out;

class Insect 
{
	private int i = 1;
	protected int j;
	private static int x1 = printInit("static Insect.x1 initialized");
	
	Insect() 
	{
		out.println("Insect constructor");
		out.println("i = " + i + ", j = " + j + ", x1 = " + x1);
		this.j = 2;
	}
	
	static int printInit(String s) 
	{
		out.println(s);

		return 3;
	}
}

public class Beetle extends Insect 
{
	private int k = printInit("Beetle.k initialized");
	private static int x2 = printInit("static Beetle.x2 initialized");
	
	public Beetle() 
	{
		out.println("Beetle constructor");
		out.println("j = " + j + ", k = " + k + ", x2 = " + x2);
	}
 
	public static void main(String[] args) 
	{
		Beetle b = new Beetle();
	}
}
//output:
/*
	static Insect.x1 initialized
	static Beetle.x2 initialized
	Insect constructor
	i = 1, j = 0, x1 = 3
	Beetle.k initialized
	Beetle constructor
	j = 2, k = 3, x2 = 3
*/

  ->首先,访问Beetle.main(),是个static,好,加载Beetle.class;

    -->发现Beetle extends Insect,好,加载Insect.class(如果Insect还有base class,则持续这一过程,直到找到root base class);

      ---->初始化private static int x1( = 3),打印"static Insect.x1 initialized";

      ---->Insect.class加载完毕

    -->继续加载Beetle.class;

      ---->初始化private static int x2( = 3),打印"static Beetle.x2 initialized";

      ---->Beetle.class加载完毕,

  ->开始执行Beetle.main();

    -->要new一个Beetle,须先new一个Insect;

      ---->在执行Insect constructor前,先要初始化member,由于private static int x1是static且已经初始化了,所以这次只初始化private int i( = 1)和protected int j( = 0 by default);

      ---->执行Insect constructor,打印"Insect constructor"和"i = 1, j = 0, x1 = 3",然后j = 2;

      ---->Insect constructor执行完毕,即new Insect过程完毕

    -->继续new Beetle;

      ---->同理,在执行Beetle constructor之前,要先初始化member,这里是初始化private int k( = 3),打印"Beetle.k initialized";

      ---->执行Beetle constructor,打印"Beetle constructor"和"j = 2, k = 3, x2 = 3";

      ---->Beetle constructor执行完毕,即new Beetle过程完毕

  ->Beetle.main()执行完毕。

 

(2009年03月27日补充:more details see [#0x000B])

(2009年09月04日归纳:[#0x0023])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值