package com.moxi.wc; import static java.lang.Math.random; /** * @author Mr.Wang * @version 1.0 * @since 1.8 */ public class StaticDemo { public static void main(String[] args) { // 靜態導入的方法在主函數中直接調用,在類裏也是一樣的 System.out.println(random());; // 類中執行順序: 靜態代碼塊 匿名代碼塊 構造函數 Demo demo = new Demo(); } } class Demo{ int b = 2; /*System.out.println("我也代碼塊"); */ // 類中不能直接寫打印語句,只能有屬性和方法 static { System.out.println("我是靜態代碼塊"); } public Demo() { System.out.println("我是構造函數"); } { int a = 1; System.out.println("我是匿名代碼塊"); System.out.println("我打印a:" + a); // 我打印a:1 System.out.println("我打印b:" + b); // 我打印b:2 } int a = 1; // 如果把a的定義寫在這上面的匿名代碼塊會報錯的 }