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

Slides:



Advertisements
Similar presentations
Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
Advertisements

Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
: Arrange the Numbers ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11481: Arrange the Numbers 解題者:李重儀 解題日期: 2008 年 9 月 13 日 題意: 將數列 {1,2,3, …,N}
Introduction to Java Programming Lecture 13 Classes I OO Programming.
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/3 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH3.5 ~ CH /10/31.
3Com Switch 4500 切VLAN教學.
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
Introduction to Constructor, Overloading, and Accessibility CS340100, NTHU Yoshi.
YOSHI Package and Import. 加油 ! 陳欣怡 ! 世界上有多少陳欣怡 ? 2004 的大學聯考,榜單上叫「陳怡君」的,總共有 64 人,有些人考上了好學校,有些卻名落孫山,同樣 姓名卻有不一樣的際遇。 雅婷.勝.
期末專題 - 吊人頭遊戲 第 35 組 組員 : 電機系 49841XXXX XXX 電機系 49841OOOO OOO.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
自由進入及離開. 定義  長期 ─ 是指生產者能夠改變所有生產因素的情況。  自由進入及離開 ─ 是指公司能夠自由進入及離開市場而不受限 制。
Review of Chapter 3 - 已學過的 rules( 回顧 )- 朝陽科技大學 資訊管理系 李麗華 教授.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
: Problem E - Beverages ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11060: Problem E - Beverages 解題者:李重儀 解題日期: 2008 年 3 月 4 日 題意: Dilbert 大學剛畢業,他決定要和朋友出去。他有奇怪的嗜好,
: Matrix Decompressing ★★★★☆ 題組: Contest Volumes with Online Judge 題號: 11082: Matrix Decompressing 解題者:蔡權昱、劉洙愷 解題日期: 2008 年 4 月 18 日 題意:假設有一矩陣 R*C,
第 13 章 常數、靜態成員與朋友關 係 13-1 常數物件與成員 13-1 常數物件與成員 13-2 物件成員與巢狀類別 13-2 物件成員與巢狀類別 13-3 靜態成員資料與函數 13-3 靜態成員資料與函數 13-4 朋友關係 13-4 朋友關係.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
Hint of Homework 4 jinnjy. Outline Hint of exercise 3.18.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Introduction to Java Programming Lecture 15 Objects and Classes.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
:Problem D: Bit-wise Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10232: Problem D: Bit-wise Sequence 解題者:李濟宇 解題日期: 2006 年 4 月 16.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
: Tight words ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: : Tight Words 解題者:鐘緯駿、林一帆 解題日期: 2006 年 03 月 14 日 題意: 給定數字 k 與 n (0 ≦ k.
類別函式庫. Java Bean 一個單獨的 AWT 元件都是一個 bean. Java Bean 也有一個 bean 的 class 可繼承, 但是並不強 迫一定要繼承. Bean 的結構 Properties 屬性 : 描述 bean 的屬性 如顏色, 大小,label 等. 透過一對 methods.
電腦優化 黃柏漢 羅元富 黃得瑋.
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
短缺,盈餘與均衡. 遊戲規則  老師想出售一些學生喜歡的小食。  老師首先講出價錢,有興趣買的請舉手。
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
公用品.  該物品的數量不會因一人的消費而受到 影響,它可以同時地被多人享用。 角色分配  兩位同學當我的助手,負責:  其餘各人是投資者,每人擁有 $100 , 可以投資在兩種資產上。  記錄  計算  協助同學討論.
: Problem G e-Coins ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10306: Problem G e-Coins 解題者:陳瀅文 解題日期: 2006 年 5 月 2 日 題意:給定一個正整數 S (0
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/25 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH 2.4~CH 2.6 &
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
: Expect the Expected ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11427: Expect the Expected 解題者:李重儀 解題日期: 2008 年 9 月 21 日 題意:玩一種遊戲 (a game.
-Antidifferentiation- Chapter 6 朝陽科技大學 資訊管理系 李麗華 教授.
逆向選擇和市場失調. 定義  資料不對稱 在交易其中,其中一方較對方有多些資料。  逆向選擇 出現在這個情況下,就是當買賣雙方隨意在 市場上交易,與比較主動交易者作交易為佳 。
: Flip Sort ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10327: Flip Sort 解題者:歐子揚 解題日期: 2010 年 2 月 26 日 題意:在這個問題中使用一種排序方式 (Flip) ,意思就是 只能交換相鄰的.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Introduction to Java Programming Lecture 9 Flow Control : while do-while and for loops II.
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
1 Introduction to Java Programming Lecture 6 Program Input : BufferedReader & Scanner.
Extreme Discrete Summation ★★★★☆ 題組: Contest Archive with Online Judge 題號: Extreme Discrete Summation 解題者:蔡宗翰 解題日期: 2008 年 10 月 13 日.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
: Problem E Antimatter Ray Clearcutting ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11008: Problem E Antimatter Ray Clearcutting 解題者:林王智瑞.
轉寄來的信件. Sometimes, we think of why friends keep forwarding mails to us without writing a word, maybe this could explain why.. 有時我們會想, 為什麼朋友總是不斷轉寄信件給我們但卻沒有.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/30 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH7.1~CH /12/26.
: Wine trading in Gergovia ★★☆☆☆ 題組: Contest Volumes with Online Judge 題號: 11054: Wine trading in Gergovia 解題者:劉洙愷 解題日期: 2008 年 2 月 29 日 題意:在 Gergovia.
幼兒行為觀察與記錄 第十章 軼事記錄法.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
: SAM I AM ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11419: SAM I AM 解題者:李重儀 解題日期: 2008 年 9 月 11 日 題意: 簡單的說,就是一個長方形的廟裡面有敵人,然 後可以橫的方向開砲或縱向開砲,每次開砲可以.
Introduction to Java Programming Lecture 16 Super and Sub Classes.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
Java Tutorial /10/21. Java Resource Java SDK –Download from –Install Jdk windows-i586.exe –
Presentation 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 = ; } 我們會使用 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() ); // 印出 雖然我們也可以產生實體後再以實體的 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?

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

Homework

References lang/Class.html lang/Class.html