静态初始化块的理解

上传人:夏** 文档编号:478614677 上传时间:2022-09-04 格式:DOCX 页数:5 大小:10.07KB
返回 下载 相关 举报
静态初始化块的理解_第1页
第1页 / 共5页
静态初始化块的理解_第2页
第2页 / 共5页
静态初始化块的理解_第3页
第3页 / 共5页
静态初始化块的理解_第4页
第4页 / 共5页
静态初始化块的理解_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《静态初始化块的理解》由会员分享,可在线阅读,更多相关《静态初始化块的理解(5页珍藏版)》请在金锄头文库上搜索。

1、静态初始化块的理解java静态初始化块是什么? static void haha ( /具体的内容调用一个 类时,该静态块首先调用,并且只调用一次,再调用构造器。程序如下:public class Teststaticblock ( public Teststaticblock() ( this(second); &java静态初始化块是什么?static void haha(具体的内容调用一个类时,该静态块首先调用,并且只调用一次,再调用构造器。程序如下:public class Teststaticblock(public Teststaticblock()(this(second);Sy

2、stem.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);Sy

3、stem.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);/ Syste

4、m.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 contexts_a=11111;s_b=22222;/ c=33333;/non-static variable c cannot be referenced from a static context/ d=44444;/non-static variable c c

5、annot be referenced from a static contextSystem.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 contextSystem.out.println(before new cla

6、ss 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 f

7、rom a static contexts_a=111111;s_b=222222;/c=333333;/non-static variable c cannot be referenced from a staticcontext/d=444444;/non-static variable c cannot be referenced from a staticcontextSystem.out.println(s_a);System.out.println(s_b);/ System.out.println(c);/non-static variable c cannot be refer

8、enced from a static context/ System.out.println(d);/non-static variable c cannot be referenced from a static contextSystem.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);/i

9、llegal forward references_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 referenceSystem.out.println(end block);static(System.out.println(begin static block);System.out.println(s_a);/System.out.println(s_b);/il

10、legal forward reference/System.out.println(c);/non-static variable c cannot be referencedfrom a static context/ System.out.println(d);/non-static variable c cannot be referenced from a static contexts_a=11;s_b=22;System.out.println(s_a);/System.out.println(s_b);/illegal forward reference/System.out.

11、println(c);/non-static variable c cannot be referencedfrom a static context/ System.out.println(d);/non-static variable c cannot be referenced from a static contextSystem.out.println(end static block);int d=4;static int s_b=2;输出如下:begin static block111end static blockbegin main1121111122222before ne

12、w class objectbegin block11111222223111222333end blockbegin second constructorend second constructorbegin constructor11122233341111222233334444end constructorend new class object11112222111111222222end main通过对输出进行分析,可以得出如下结果:1、在类第一次加载时候,会执行静态域(field)初始化语句和静态块(用static 包含的部分)。这里要注意:a、不管静态域声明语句的实际位置在哪儿

13、,当第一次加载类的时候都会首先对它初始化为缺省值(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

14、=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下编译将不允许在静态块或实例块中改变位置在它们 后面声 明的域的值。原文出处:中软卓越 http:/http:/

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号