public class codeDemo1 { public static String schoolName; public static String[] cards = new String[54]; static{ System.out.println("===静态代码块执行了==="); schoolName = "牛马程序员"; cards[0] = "A"; cards[1] = "2"; cards[1] = "3"; // ... }
public static vod main(String[] args){ System.out.println("===main方法块执行了==="); System.out.println(schoolName);
System.out.println(Arrays.toString(cards)); } }
实例代码块
[Java] [实例代码块]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class codeDemo2 { private String name; private String[] direction = new String[4]; //实例变量 { System.out.println("===实例代码块执行了==="); name = "Benjamin"; direction[0] = "N"; direction[1] = "S"; direction[2] = "E"; direction[3] = "W"; }
public static vod main(String[] args){ System.out.println("===main方法块执行了==="); new codeDemo2(); // 创建对象时自动执行实例代码块 new codeDemo2(); new codeDemo2(); } }