class StaticCode
{
static
{
System.out.println("c");
}
{
System.out.println("d");
}
public static void show()
{
System.out.println("show run");
}
}
class StaticCodeDemo
{
static
{
System.out.println("a");
}
public static void main(String[] args)
{
new StaticCode(); //不同点
System.out.println("Hello World!");
}
static
{
System.out.println("b");
}
}
运行结果:
a
b
c
d
Hellow World
class StaticCode
{
static
{
System.out.println("c");
}
{
System.out.println("d");
}
public static void show()
{
System.out.println("show run");
}
}
class StaticCodeDemo
{
static
{
System.out.println("a");
}
public static void main(String[] args)
{
StaticCode.show();//不同点
System.out.println("Hello World!");
}
static
{
System.out.println("b");
}
}
运行结果
a
b
c
show run
Hellow World
下边的为什么执行不到“d”??不是说Static代码块会随着类的存在而执行吗
|