static int x = 1, { //block float sss = 333.3; String str = "hello"; } |
static { //(static block), int x = 2; double y = 33.3; } |
/** *//** * * @author livahu * Created on 2006年9月6日, 下午17:00 */ class FirstClass ...{ FirstClass(int i) ...{ System.out.println("FirstClass(" + i + ")"); } void useMethod(int k) ...{ System.out.println("useMethod(" + k + ")"); } } 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! |