Just watching http://community.9cbs.net/expert/topic/2872/2872127.xml This very but very meaningful post, see a piece of code inside about discussing the following code: Class Singleton {Private Static Singleton Obj = New Singleton (); Public Static Int Counter1; Public Static Int Counter2 = 0; Private Singleton () {//system.out.println ("counter1: " Counter1 " / N " " Counter2: " Counter2); Counter1 ; counter2 ; //system.out.println ("counter1: " counter1 " / n " " counter2: " co Unter2);} public static singleleton getInstance () {return}}
Public class mymain {public static void main (string [] args) {singleton obj = singleton.getinstance (); system.out.println ("obj.counter1 ==" obj.counter1); system.out.println (" Obj.counter2 == " Obj.counter2);} This time the result is: obj.counter1 == 1obj.counter2 == 0 Remove the results before the two debug strategies before the code is: Counter1: 0counter2: 0counter1: 1counter2: 1obj.counter1 == 1obj.counter2 == 0 Change the program to make the program, you will understand the order of compilation: Class starting { Private static singleleton obj = new singleleton (); public static int counter2; // Start Change Static {counter2 = 1;} // Change End Private Singleton () {System.out.Println ("Counter1:" Counter1 "/ N" "Counter2:" counter2);
Counter1 ; counter2 ; system.out.println ("counter1:" counter1 / n " " counter2: " counter2);} public static singleleton getInstance () {return obj;}}