Download Static***

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
What is static?
CS340100, NTHU
Yoshi
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
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
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
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
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;
}
}
Example2: 數學
class Math {
public static final double PI = 3.1415926;
}
• 我們會使用 Math.PI 來取得這個公定的常數
用在 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;
}
}
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()
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?
Overriding in Static Method?
• No overriding in static method
– It is known as hiding
– Which is executed is according to the type
Discussion
• 為什麼我compile常看到compiler說,你不能
在static method存取non-static variables?
– Static method本來就存在,實體不存在當然不
能存取
– 即使實體存在,也不知道你想存取”哪一個實體”
• 為什麼main要宣告成static?
– 一開始總要有個進入點,讓程式開始執行
– 如果不使用static修飾字,則一定要new了之後
產生實體才能使用,那麼就無從開始了
更Advance的問題
• ClassLoader?
• 有一個class,名字叫做java.lang.Class (注意
大小寫)
– 前者是說”有一個類別”
– 後者是說”這個類別名字叫java.lang.Class”
• 有一種寫法
– Class klass = Class.forName(“java.lang.String”);
– klass.newInstance();
• Why?
Memory
An instance of
class XXX
Class XXX
{ Static fields
Static methods }
New/instantiate
XXX.class
An instance of
class XXX
An instance of
class XXX
Homework
References
• http://java.sun.com/j2se/1.4.2/docs/api/java/
lang/Class.html