public Example(){ stra = “Welcome To Constructor”; System.out.println(“构造函数被调用”); } public show(){ System.out.println(stra); } pubilc static void main(String[] args){ Example example = new Example();
example.show; } }运行结果: 静态初始块被调用 初始块被调用 构造函数被调用 Welcome To Constructor希望你能明白!
String stra = new String(“Hello World”); {
stra = “Hello”;
System.out.println(“初始块被调用”);
} static{
stra = “STATIC”;
System.out.println(“静态初始块被调用”);
}
public Example(){
stra = “Welcome To Constructor”;
System.out.println(“构造函数被调用”);
} public show(){
System.out.println(stra);
} pubilc static void main(String[] args){
Example example = new Example();
example.show;
}
}运行结果:
静态初始块被调用
初始块被调用
构造函数被调用
Welcome To Constructor希望你能明白!