Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 714|回复: 0

静态初始化块的理解

[复制链接]

该用户从未签到

发表于 2011-9-13 14:54:03 | 显示全部楼层 |阅读模式
java静态初始化块是什么??
static void haha
{
//具体的内容
}
调用一个类时,该静态块首先调用,并且只调用一次,再调用构造器。
程序如下:
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下编译将不允许在静态块或实例块中改变位置在它们 后面声明的域的值。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-5 09:03 , Processed in 0.311983 second(s), 38 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表