class Chinese
{
private static int count =0; static
{
count =2;
System.out.println("static code");
} private Chinese()
{
System.out.println(++count);
}static void sing()
{
System.out.println("啊!");
}
void singOurCountry()
{
System.out.println(country);
}
}class TestChinese
{
public static void main(String [] args)
{ System.out.println("begin");
Chinese.sing();
System.out.println("end");
new Chinese().sing();
new Chinese().singOurCountry();
new Chinese(); }}此段代码的运行结果为:beginstatic code啊!end3啊!4中国5 不明白为什么运行到Chinese.sing();时输出了static code望高手指点啊····
static 块仅在该类被加载时执行一次
class UseStatic {一旦UseStatic 类被装载,所有的static语句被运行。首先,a被设置为3,接着static 块执行(打印一条消息),最后,b被初始化为a*4 或12。然后调用main(),main() 调用meth() ,把值42传递给x。3个println ( ) 语句引用两个static变量a和b,以及局部变量x 。 注意:在一个static 方法中引用任何实例变量都是非法的。
下面是该程序的输出:
Static block initialized.
x = 42
a = 3
b = 12