zoukankan      html  css  js  c++  java
  • java静态初始化块(静态域)

    1. 类变量的初始化可通过静态初始化块来进行。

        代码放在一对大括号内,大括号前用static修饰:static {……}

        一个类可定义1个或多个静态初始化块。

      静态初始化块会在加载时调用而且只被调用一次。

       属于类的公共域,为该类所有对象共享。so它可看做是类范围内的一种的全局变量。

       静态域的值由静态方法改变。

    class A {
    	public A() {System.out.println("A's constructor!");}
    }
    class TestStaticBlock extends A {
    	//constructor 
    	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");
    	}
    	//second constructor
    	public TestStaticBlock(String s) {
    		System.out.println("begin second constructor");
    		System.out.println("end second constructor");
    	}
    	//main
    	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");
    	}
    	//define static s_a
    	static int s_a = 1;
    	//define c
    	int c = 3;
    
    	//block
    	{
    		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 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 d cannot be referenced
    		// from a static context
    		s_a = 11;//这个s_a之前定义过
    		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");
    	}
    	//define d
    	int d = 4;
    	//define static s_b
    	static int s_b = 2;
    }
    

    输出结果:  

    begin static block
    1
    11
    end static block
    begin main
    11
    2
    11111
    22222
    before new class object
    A's constructor!
    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下编译将不允许在静态块或实例块中改变位置在它们后面声明的域的值。(看下面的例子,可以知道,core java 2中说得没错,进入构造方法,没错.如果第一句是调用别的构造方法(包括因为继承了父类而有个隐形的构造方法),则就进入别的构造方法,否则,先执行实例块,再回到构造方法里执行往下的内容.事实上已经进入了构造方法之后,先执行实例块,再执行构造方法.而不是反驳者所说的:"事实是,不管是否使用this()都会先执行实例块,再进入构造方法。"

    在看一个例子:

    public class TestExtend4 {
    	public static void main(String[] args) {
    		C c = new C(); //1
    	}
    }
    class A {
    	static {
    		System.out.println("1"); //2
    	}
    	public A() { //7
    		System.out.println("2"); //8
    	}
    }
    
    class B extends A {
    	static {
    		System.out.println("a"); //3
    	}
    	public B() { //6
    		System.out.println("b"); //9
    	}
    }
    
    class C extends B {
    	static {
    		System.out.println("I"); //4
    	}
    	public C() { //5
    		System.out.println("II"); //11
    	}
    
    	{
    		System.out.println("yeah!"); //10
    	}
    }
    

      结果:
    1
    a
    I
    2
    b
    yeah!
    II

    在eclipse中用debug运行可以看得出这段程序的有标有断点的语句的执行顺序,如上面序号所示.可以总结出某些继承和静态的知识:

    1.当要载入类时(注意只会载入一次),先初始化类里的静态域声明变量为默认值,(即使是赋值了,int值也要为默认的0)然后按顺序的执行静态域和静态块.
    2.但是当此类有父类时,要先如果父类有静态域或者静态块,先执行父类的,再执行子类的.
    3.当执行完有关静态的东西时,就到了构造类实例时候,会先对实例域初始化为缺省值,然后执行实例块(用{}括起来的部分),然后执行构造方法。
    4.但是当此类有父类时,就应该先进入父类的构造方法执行内容,然后再回到子类构造方法里,然后再跳出子类构造方法,执行子类里的实例域和实例块,然后再回到子类构造方法里,执行子类构造方法里面的内容.(就像上面所说的:《Core java 2》书中所说的"进入构造方法后,如果第一句是调用别的构造方法,则进入别的构造方法。否则,执行实例块")意思是无论其是不是继承了父类,还是调用了自己类的另一个构造方法,都会先执行,否则,如果有实例块就先执行实例块.最后再执行构造方法里面的内容.

    Thinking in Java 第七章 里面的例子:

    class Meal {
    	Meal() {
    		System.out.println("Meal()");
    	}
    }
    class Bread {
    	Bread() {
    		System.out.println("Bread()");
    	}
    }
    class Cheese {
    	Cheese() {
    		System.out.println("Cheese()");
    	}
    }
    class Lettuce {
    	Lettuce() {
    		System.out.println("Lettuce()");
    	}
    }
    class Lunch extends Meal {
    	Lunch() {
    		System.out.println("Lunch()");
    	}
    }
    class PortableLunch extends Lunch {
    	PortableLunch() {
    		System.out.println("PortableLunch()");
    	}
    }
    public class Sandwich extends PortableLunch {
    	private Bread b = new Bread();
    	private Cheese c = new Cheese();
    	private Lettuce l = new Lettuce();
    	public Sandwich() {
    		System.out.println("Sandwich()");
    	}
    
    	{
    		System.out.println("yeah");
    	}
    	public static void main(String[] args) {
    		new Sandwich();
    	}
    }
    

     输出结果: 

    Meal()
    Lunch()
    PortableLunch()
    Bread()
    Cheese()
    Lettuce()
    yeah
    Sandwich()

    说明: Sandwich是public主类,反映三层级别的继承(若将从Object的隐含继承也算在内,就是四级),构造此复杂对象调用构造器要遵照下面的顺序:
    1.调用基类构造器.这个步骤会不断地反复递归下去,首先是构造这种层次结构的根,然后是下一层导出类,等等.直到最底层的导出类.
    2.按声明顺序调用成员的初始状态设置模块(先初始化类成员变量为默认值(即使有设置值也要设置成默认值,譬如int x = 3;的话,x初始化为0.),然后按照代码顺序执行实例域和实例块)
    3.最后执行此类构造方法里面的内容.

    最后说明一点:

    静态初始化器对静态域进行初始化,构造方法也是完成初始化工作。但二者本质不同,静态初始器是类进入内存时,系统调用执行,而构造方法是执行new后自动执行。

  • 相关阅读:
    c语言函数定义、函数声明、函数调用以及extern跨文件的变量引用
    c语言局部变量 静态局部变量 全局变量与静态全局变量
    Source Insight建工程之Kernel
    day49
    管道实例取出最常用的10个指令
    day52
    Linux中vi的使用
    vi高级技巧
    vim配置
    typedef
  • 原文地址:https://www.cnblogs.com/little-white/p/3441483.html
Copyright © 2011-2022 走看看