Static块:该类的任何方法被首次触碰到时(马克-to-win: when you touch Test的main方法时),Static块被运行。可以在里面初始化你的static变量,不能访问实例变量。在所有静态变量初始化之后运行,见例子。
class Test1{
static {
System.out.println("Static block Test1 initialized.");
}
}
public class Test {
/*下面两句话是在静态块儿之前执行,所以它的值,被静态块儿里面赋的值所覆盖掉。马克-to-win, the following two statements are before the execution of the static block.*/
static int a = 3;
static int b;
int c;
static void cal(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
// 静态块儿Static block
static {
// c=9; 是错误的,will cause an error.
System.out.println("Static block initialized.");
a = 9;
b = a * 4;
System.out.println("a = " + a);
System.out.println("b = " + b); }
public static void main(String args[]) {
System.out.println("in main");
。。。。。。。。。。。。。。。。。。。
详情请见:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner2_web.html#StaticBlock
继续阅读与本文标签相同的文章
-
新能力丨困扰商家已久的“分账问题”终于被解决了!
2026-05-18栏目: 教程
-
在线PDF加密,你的隐私你做主!
2026-05-18栏目: 教程
-
浅谈物联网用户体验目标的变化
2026-05-18栏目: 教程
-
Linux基础命令---host域名查询工具
2026-05-18栏目: 教程
-
Apache Flink Meetup 北京站,可能有你最想听的技术干货!
2026-05-18栏目: 教程
