java 启动顺序_java语句执行顺序

-

在《Core java 2: volumn 1, Edition

5》一书的第四章“对象与类”中讲到域赋值语句、实例块、静态块及构造方法等在创建类实例时的执行顺序,中文译本有些处翻译的不贴切,而英文原书中也有一处错误。本文通过一个小程序来说明类实例构造过程中的语句执行顺序。

程序如下:

public class Teststaticblock

{

public Teststaticblock()

{

this("second");

System.out.println("begin

constructor");

System.out.println(s_a);

System.out.println(s_b);

System.out.println(c);

System.out.println(d);

// this("second");//call to this

must be first statement in constructor

s_a=1111;

s_b=2222;

c=3333;

d=4444;

System.out.println(s_a);

System.out.println(s_b);

System.out.println(c);

System.out.println(d);

System.out.println("end

constructor");

}

public Teststaticblock(String s)

{

System.out.println("begin

second constructor");

System.out.println("end second

constructor");

}

public static void main(String args[])

{

System.out.println("begin

main");

System.out.println(s_a);

System.out.println(s_b);

// System.out.println(c);//non-static

variable c cannot be referenced from a static context

// System.out.println(d);//non-static

variable c cannot be referenced from a static context

s_a=11111;

s_b=22222;

// c=33333;//non-static variable

c cannot be referenced from a static context

// d=44444;//non-static variable

c cannot be referenced from a static context

System.out.println(s_a);

System.out.println(s_b);

// System.out.println(c);//non-static

variable c cannot be referenced from a static context

// System.out.println(d);//non-static

variable c cannot be referenced from a static context

System.out.println("before new

class object");

Teststaticblock t = new

Teststaticblock();

System.out.println("end new

class object");

System.out.println(s_a);

System.out.println(s_b);

// System.out.println(c);//non-static

variable c cannot be referenced from a static context

// System.out.println(d);//non-static

variable c cannot be referenced from a static context

s_a=111111;

s_b=222222;

// c=333333;//non-static

variable c cannot be referenced from a static context

// d=444444;//non-static

variable c cannot be referenced from a static context

System.out.println(s_a);

System.out.println(s_b);

// System.out.println(c);//non-static

variable c cannot be referenced from a static context

// System.out.println(d);//non-static

variable c cannot be referenced from a static context

System.out.println("end

main");

}

static int s_a=1;

int c=3;

{

System.out.println("begin

block");

System.out.println(s_a);

System.out.println(s_b);

System.out.println(c);

// System.out.println(d);//illegal

forward reference

s_a=111;

s_b=222;

c=333;

d=444;

System.out.println(s_a);

System.out.println(s_b);

System.out.println(c);

// System.out.println(d);//illegal

forward reference

System.out.println("end

block");

}

static

{

System.out.println("begin

static block");

System.out.println(s_a);

// System.out.println(s_b);//illegal

forward reference

// System.out.println(c);//non-static

variable c cannot be referenced from a static context

// System.out.println(d);//non-static

variable c cannot be referenced from a static context

s_a=11;

s_b=22;

System.out.println(s_a);

// System.out.println(s_b);//illegal

forward reference

// System.out.println(c);//non-static

variable c cannot be referenced from a static context

// System.out.println(d);//non-static

variable c cannot be referenced from a static context

System.out.println("end static

block");

}

int d=4;

static int s_b=2;

}

输出如下:

begin static block

1

11

end static block

begin main

11

2

11111

22222

before new class object

begin block

11111

22222

3

111

222

333

end block

begin second constructor

end second constructor

begin constructor

111

222

333

4

1111

2222

3333

4444

end constructor

end new class object

1111

2222

111111

222222

end main

通过对输出进行分析,可以得出如下结果:

1、在类第一次加载时候,会执行静态域(field)初始化语句和静态块(用static{}包含的部分)。

这里要注意:

a、不管静态域声明语句的实际位置在哪儿,当第一次加载类的时候都会首先对它初始化为缺省值(0,false,null等)。

b、即使静态域声明中使用了显式初始化语句(比如:int

x=3),第一次加载类的时候也会先把它初始化为缺省值(此时x为0),然后再按照下面说的要点c来执行赋值语句(x=3)。

c、对于静态域的显式初始化语句和静态块,按照在类中代码出现的先后顺序执行。

因此,在上面的例子程序中,我们看到

static int s_a=1;

static

{

s_a=11;

s_b=22;

}

static int s_b=2;

对s_a,s_b会有不同的效果。类加载时候,s_a,s_b都被初始化为0,然后由于依照代码顺序执行了s_a=1;s_a=11;s_b=22;s_b=2;结果s_a、s_b分别变成了11和2。

2、当构造类实例时候,会先对实例域初始化为缺省值,然后执行实例块(用{}括起来的部分),然后执行构造方法。其中:

a、如同1中一样,如果有实例域的显式初始化语句,程序仍然是先将该域初始化为缺省值,然后按照代码在类中出现的先后顺序执行初始化语句或者实例块。如果实例块位置在初始化语句前面,即使它改变了该域的值,也会被随后执行的初始化语句改回去。

b、在进入构造方法后,如果构造方法第一句是使用this(...)调用另一构造方法的话,则先执行另一构造方法,然后再执行本构造方法的方法体。这种用法必须让this(...)位于第一句。

《Core java

2》书中所说的"进入构造方法后,如果第一句是调用别的构造方法,则进入别的构造方法。否则,执行实例块"的提法有问题。事实是,不管是否使用this()都会先执行实例块,再进入构造方法。另外,本程序需要在sdk1.4下编译,在sdk1.3下编译将不允许在静态块或实例块中改变位置在它们后面声明的域的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值