1 / 11

What is static?

What is static?. Static?. 靜態 ? class Test { static int staticX ; int instanceX ; public Test( int var1, int var2) { this.staticX = var1; this.instanceX = var2; } } 差別在於, instanceX 是一個 ”instance member” ,而 staticX 是一個 ”class member”. 當我們產生很多物件時 …. Test obj1 = new Test(1,2);

galena
Download Presentation

What is static?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What is static?

  2. Static? • 靜態? class Test { static intstaticX; intinstanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX = var2; } } • 差別在於,instanceX是一個”instance member”,而staticX是一個”class member”

  3. 當我們產生很多物件時… • Test obj1 = new Test(1,2); • Test obj2 = new Test(3,4); • Test obj3 = new Test(5,6); • 物件obj1, obj2, obj3所指向的實體,會各自有各自的instanceX • System.out.println(obj1.instanceX); //印出2 • System.out.println(obj2.instanceX); //印出4 • System.out.println(obj3.instanceX); //印出6

  4. 但static variable不同 • Static variable不屬於任何一個instance • 上述的程式順著執行下來,staticX的值被依序改成1,3,5 • 他們改的是同一份! • Static variable屬於 “class所有” • this.staticX = 3,其實等價於寫 Test.staticX = 3

  5. 什麼時候用得到? • 建立一個class來代表銀行的帳戶 • 每個帳戶有名字,有他們存的錢的數量,以及銀行利率 • 每個人的戶名不同 • 每個人存的錢數量不同 • 銀行利率是統一的! 大家都一樣

  6. Example: Account.java class Account { int money; String name; static double rate = 0.002; //0.2% public Account(String name, int money) { this.name = name; this.money = money; } }

  7. Example2: 數學 class Math { public static final double PI = 3.1415926; } • 我們會使用 Math.PI來取得這個公定的常數

  8. 用在 Method 時呢? • 不需要有instance,即可以使用 • 再看Account的例子 class Account { private int money; private String name; private static double rate = 0.002; //0.2% public Account(String name, int money) { this.name = name; this.money = money; } public static double getRate() { return rate; } }

  9. Static method • 不存在任何實體即可使用,則 • System.out.println( Account.getRate() ); //印出0.002 • 雖然我們也可以產生實體後再以實體的reference variable來存取 • Account acc1 = new Account(“John”, 1000); • System.out.println( acc1.getRate() ); • 但是這樣的寫法是”不被建議的”,造成誤解,雖然compile可以過,但Eclipse會警告,要你改成Account.getRate()

  10. Discussion • 為什麼我compile常看到compiler說,你不能在static method存取non-static variables? • Static method本來就存在,實體不存在當然不能存取 • 即使實體存在,也不知道你想存取”哪一個實體” • 為什麼main要宣告成static? • 一開始總要有個進入點,讓程式開始執行 • 如果不使用static修飾字,則一定要new了之後產生實體才能使用,那麼就無從開始了

  11. 更Advance的問題 • ClassLoader? • 有一個class,名字叫做java.lang.Class (注意大小寫) • 前者是說”有一個類別” • 後者是說”這個類別名字叫java.lang.Class” • 有一種寫法 • Class klass = Class.forName(“java.lang.String”); • klass.newInstance(); • Why?

More Related