class HelloA { public HelloA() {
System.out.println("HelloA");
} { System.out.println("I'm A class"); } static { System.out.println("static A"); } } public class HelloB extends HelloA {
public HelloB() {
System.out.println("HelloB");
} { System.out.println("I'm B class"); } static { System.out.println("static B"); } public static void main(String[] args) { System.out.println("-------main start-------");
new HelloB();
new HelloB();
System.out.println("-------main end-------");
}
}
输出的结果:
static A
static B
-------main start-------
I'm A class
HelloA
I'm B class
HelloB
I'm A class
HelloA
I'm B class
HelloB
-------main end-------
public class HelloA { public HelloA() {
System.out.println("HelloA");
} {
System.out.println("I'm A class");
} static {
System.out.println("static A");
}
} public class HelloB extends HelloA { private HelloB() {
System.out.println("HelloB");
} public static HelloB getInstance(){
return TestHolder.instance;
} {
System.out.println("I'm B class");
} static {
System.out.println("static B");
} public static void main(String[] args) {
System.out.println("-------main start-------");
// new Test();
// new Test();
HelloB.getInstance();
HelloB.getInstance(); System.out.println("-------main end-------");
} public static class TestHolder{
static {
System.out.println("static TestHolder");
} {
System.out.println(" TestHolder.................");
}
public static HelloB instance =new HelloB();
}
}
输出:
static A
static B
-------main start-------
static TestHolder
I'm A class
HelloA
I'm B class
HelloB
-------main end-------