Java 中可以通过初始化块进行数据赋值。如:
id="iframe_0.7798676821403205" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img.mukewang.com/5392da9600010e5503680168.jpg?_=5374423%22%20style=%22border:none;max-width:655px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7798676821403205',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 368px; height: 168px;">
在类的声明中,可以包含多个初始化块,当创建类的实例时,就会依次执行这些代码块。如果使用 static 修饰初始化块,就称为静态初始化块。
需要特别注意:静态初始化块只在类加载时执行,且只会执行一次,同时静态初始化块只能给静态变量赋值,不能初始化普通的成员变量。
我们来看一段代码:
id="iframe_0.9684930958319455" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img.mukewang.com/53941e320001fdd507670575.jpg?_=5374423%22%20style=%22border:none;max-width:655px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9684930958319455',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 655px; height: 491px;">
运行结果:
id="iframe_0.1000780239701271" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img.mukewang.com/53941e880001cb8003530223.jpg?_=5374423%22%20style=%22border:none;max-width:655px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1000780239701271',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-style: none; border-width: initial; width: 353px; height: 223px;">
通过输出结果,我们可以看到,程序运行时静态初始化块最先被执行,然后执行普通初始化块,最后才执行构造方法。由于静态初始化块只在类加载时执行一次,所以当再次创建对象 hello2 时并未执行静态初始化块。
任务
小伙伴们,做个练习加深对静态初始化块的理解吧!
在编辑器中,分别通过构造方法、初始化块和静态初始化块对变量进行初始化赋值
public class HelloWorld { String name; // 声明变量name String sex; // 声明变量sex static int age;// 声明静态变量age // 构造方法 public HelloWorld() { System.out.println("通过构造方法初始化name"); name = "tom"; } // 初始化块 { System.out.println("通过初始化块初始化sex"); sex = "男"; } // 静态初始化块 static { System.out.println("通过静态初始化块初始化age"); age = 20; } public void show() { System.out.println("姓名:" + name + ",性别:" + sex + ",年龄:" + age); } public static void main(String[] args) { // 创建对象 HelloWorld hello = new HelloWorld(); // 调用对象的show方法 hello.show(); } }
参考运行结果: id="iframe_0.476974441902712" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://img.mukewang.com/5392dc2c0001ded303220111.jpg?_=5374423%22%20style=%22border:none;max-width:655px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.476974441902712',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="color: rgb(51, 51, 51); font-family: Verdana, Arial, sans-serif, 'Lucida Grande'; font-size: 13.3333px; line-height: 24px; border-style: none; border-width: initial; width: 322px; height: 111px; background-color: rgb(214, 211, 214);">