# 你真的認識Static嗎? ###### tags: `JAVA` 12/18 21:00 更新,加入老師講解的內容 ``` public class StaticTest { public static int k = 0; // 都是static就照上到下順序執行 public static StaticTest t1 = new StaticTest("t1"); // 於heap區產生屬性int j public static StaticTest t2 = new StaticTest("t2"); public static int i = print("i"); public static int n = 99; public int j = print("j"); // 1、屬性的設定若有傳入funtion最優先執行 { // 這東西叫「initializing block」 print("構造塊"); // 2、產生(new)物件時必定呼叫此block,且優先於建構方法 } static { print("靜態塊"); } public StaticTest(String str) { // 3、建構方法執行(傳入t1、t2、init) System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); ++n; ++i; } public static int print(String str) { // (傳入j、構造塊、i、靜態塊) System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); ++i; return ++n; } public static void main(String[] args) { StaticTest t = new StaticTest("init"); } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up