```java import java.util.ArrayList; import java.util.List; public class ExampleCurlyBraces { // 靜態初始化區塊,程式啟動時會優先執行的區塊 static { System.out.println("1"); } // 實例初始化,實例化物件後會執行的區塊 { System.out.println("2"); } // 類別的建構子, 是建構一個類別的骨架,實例化物件後第二順序會執行的區塊 // 若不特別寫這段語法,程式也會自動建立一個看不見的建構子 // 建構子的名稱需要跟類別名稱一樣,否則系統會報錯 public ExampleCurlyBraces() { System.out.println("3"); } //--------以下是各種常見方法(method)介紹-------- //傳入參數及回傳參數使用 int 作為範例,但實際上可以傳入,或者回傳任何物件 // 靜態方法,不回傳任何值(void) public static void testStaticVoidMethod() { System.out.println("call testStaticVoidMethod"); } // 實例方法,不回傳任何值(void) public void testNormalVoidMethod() { System.out.println("call testNormalVoidMethod"); } // 靜態方法,回傳數值 int public static int testStaticReturnIntMethod() { return 5566; } // 實例方法,回傳數值 int public int testNormalReturnIntMethod() { return 5566; } // 靜態方法,回傳數值 int,且必須帶入一個數字作為參數 public static int testStaticReturnIntMethodWithParam(int i) { return i; } // 實例方法,回傳數值 int,且必須帶入一個數字作為參數 public int testNormalReturnIntMethodWithParam(int i) { return i; } private int count = 0; // 同步區塊 public synchronized void increment() { count++; } public synchronized void decrement() { count--; } //--------各種常見方法(method)介紹結束-------- //main方法,程式執行的入口 public static void main(String[] args) { final int testValue = 100; //陣列 String[] array = new String[] { "1", "2", "3" }; // if else 區塊 if (testValue > 10) { } else { } // for loop for (int i = 0; i < testValue; i ++) { } // for loop 第二種形態 List<String> testList = new ArrayList<String>(); for (String str : testList) { } // while loop while (testValue > 10) { break; } // do while loop do { break; } while ((testValue > 10)); // switch case 形態一, 每個 case 是可以獨立括號的, 參數就不會互相干擾, // 但同樣的若不加 break, 則會一直往下方 case 內容執行 switch (testValue) { case 100: { int x = 55; System.out.println(testValue + ", " + x); break; } case 200: { int x = 66; System.out.println(testValue + ", " + x); break; } case 300: { int x = 77; System.out.println(testValue + ", " + x); break; } default: break; } // switch case 形態二, 每個 case 也可以不加括號, 但參數會互相干擾 // 但同樣的若不加 break, 則會一直往下方 case 內容執行 switch (testValue) { case 100: int x = 55; System.out.println(testValue + ", " + x); break; case 200: // 因為這邊會報錯,所以標注起來 // int x = 66; // System.out.println(testValue + ", " + x); break; case 300: // 因為這邊會報錯,所以標注起來 // int x = 77; // System.out.println(testValue + ", " + x); break; default: break; } //new class ExampleCurlyBraces example = new ExampleCurlyBraces(); //直接呼叫 static 的方法 testStaticVoidMethod(); //呼叫會回傳值的 static 方法 int result1 = testStaticReturnIntMethod(); //呼叫會回傳值的 static 方法, 但需要另外傳入一個 int 參數 int result2 = testStaticReturnIntMethodWithParam(5566); //呼叫一個需要 new class 後才可以使用的方法 example.testNormalVoidMethod(); //呼叫一個需要 new class 後才可以使用,且會回傳值的方法 int result3 = example.testNormalReturnIntMethod(); //呼叫一個需要 new class 後才可以使用,且會回傳值的方法, 但需要另外傳入一個 int 參數 int result4 = example.testNormalReturnIntMethodWithParam(5566); // try catch finally try { Integer.parseInt("5"); } catch (Exception e) { } finally { } //執行緒(thread), 可以同步多工處理程式 Thread thread1 = new Thread(new Runnable() { public void run() { for (int i = 0; i < 10000; i++) { example.increment(); } } }); // 雙大括號初始化, 不常使用這樣的寫法,可以試著使用在少量資料初始化時。 // 因為若當資料量多時使用雙大括號初始物件,反而會使版面混亂難以閱讀。 // 然後,在使用雙大括號初始化資料時, // 應該注意在適當的時候清空或重新初始化物件,以避免不必要的記憶體佔用。 List<String> myList = new ArrayList<String>() {{ add("1"); add("2"); add("3"); }}; Map<String, String> myMap = new HashMap<String, String>() {{ put("1", "abc"); put("2", "def"); put("3", "hijk"); }}; } // 內部類別 class CurlyBracesChildTest { { } public CurlyBracesChildTest() { } } } ```