Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;

Similar presentations


Presentation on theme: "What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;"— Presentation transcript:

1 What is static? CS340100, NTHU Yoshi

2 Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX = var2; } Instance members belong to the individual objects Class members belong to the class, shared by all the objects

3 When we creates many objects… Test obj1 = new Test(1,2); Test obj2 = new Test(3,4); Test obj3 = new Test(5,6); Objects (obj1, obj2, obj3) have their own instanceX – System.out.println(obj1.instanceX); //prints 2 – System.out.println(obj2.instanceX); //prints 4 – System.out.println(obj3.instanceX); //prints 6

4 Static Variable are Shared Static variables do not belong to an individual object In the code above, the value of staticX are modified to 1, 3, 5, sequentially – They were modifying the same one (they?) – Static variables belong to the class – this.staticX = 3 is equivalent to Test.staticX = 3

5 When will you declare a field as static? Let’s see an example – You are creating a class for representing the accounts of a bank Each account has the user name, the saving, and the interest rate – Each user has different name – Each user has different saving – The interest rate are identical, shared by all the accounts

6 Example: Account.java class Account { int money; String name; static double rate = 0.02; //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 So now let’s check Hello World public class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } Now you know that why the main method is declared as static?

11 Overriding in Static Method? No overriding in static method – It is known as hiding – Which is executed is according to the type

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

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

14 Class XXX { Static fields Static methods } XXX.class An instance of class XXX New/instantiate Memory

15 Homework

16 References http://java.sun.com/j2se/1.4.2/docs/api/java/ lang/Class.html http://java.sun.com/j2se/1.4.2/docs/api/java/ lang/Class.html


Download ppt "What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;"

Similar presentations


Ads by Google