What is static CS 340100 NTHU Yoshi Static

  • Slides: 16
Download presentation
What is static? CS 340100, NTHU Yoshi

What is static? CS 340100, NTHU Yoshi

Static? • 靜態? class Test { static int static. X; int instance. X; public

Static? • 靜態? class Test { static int static. X; int instance. X; public Test(int var 1, int var 2) { this. static. X = var 1; this. instance. X = var 2; } } • Instance members belong to the individual objects • Class members belong to the class, shared by all the objects

When we creates many objects… • • Test obj 1 = new Test(1, 2);

When we creates many objects… • • Test obj 1 = new Test(1, 2); Test obj 2 = new Test(3, 4); Test obj 3 = new Test(5, 6); Objects (obj 1, obj 2, obj 3) have their own instance. X – System. out. println(obj 1. instance. X); //prints 2 – System. out. println(obj 2. instance. X); //prints 4 – System. out. println(obj 3. instance. X); //prints 6

Static Variable are Shared • Static variables do not belong to an individual object

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

When will you declare a field as static? • Let’s see an example –

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 =

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; } }

Example 2: 數學 class Math { public static final double PI = 3. 1415926;

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

用在 Method 時呢? • 不需要有instance,即可以使用 • 再看Account的例子 class Account { private int money; private

用在 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 get. Rate() { return rate; } }

Static method • 不存在任何實體即可使用 – System. out. println( Account. get. Rate() ); //印出 0.

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

So now let’s check Hello World public class Hello. World. App { public static

So now let’s check Hello World public class Hello. World. App { 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

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

更Advance的問題 • Class. Loader? • 有一個class,名字叫做java. lang. Class (注意 大小寫) – 前者是說”有一個類別” – 後者是說”這個類別名字叫java.

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

Memory An instance of class XXX. class Class XXX { Static fields Static methods

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

Homework

Homework

References • http: //java. sun. com/j 2 se/1. 4. 2/docs/api/java/ lang/Class. html

References • http: //java. sun. com/j 2 se/1. 4. 2/docs/api/java/ lang/Class. html