class SecondClass ...{ static FirstClass fc1 = new FirstClass(1); FirstClass fc3 = new FirstClass(3); static ...{ FirstClass fc2 = new FirstClass(2); }
...{ System.out.println("SecondClass's block, this block is not static block."); }
SecondClass() ...{ System.out.println("SecondClass()"); } FirstClass fc4 = new FirstClass(4); }
public class InitiationDemo ...{ SecondClass sc1 = new SecondClass(); ...{ System.out.println("Hello Java World!"); }
public static void main(String[] args) ...{ System.out.println("Inside main()"); SecondClass.fc1.useMethod(100); InitiationDemo idObj = new InitiationDemo(); } static SecondClass sc2 = new SecondClass(); } 运行结果:
FirstClass(1) FirstClass(2) FirstClass(3) SecondClass's block, this block is not static block. FirstClass(4) SecondClass() Inside main() useMethod(100) FirstClass(3) SecondClass's block, this block is not static block. FirstClass(4) SecondClass() Hello Java World!