Think in Java(二):初始化與清理,thinkjava

來源:互聯網
上載者:User

Think in Java(二):初始化與清理,thinkjava
1. 區分重載方法:
參數順序的不同可以區分兩個方法,不過,一般情況下千萬別這麼做,因為這會使代碼難以維護不能通過返回值類型來區分重載方法:因為如果我直接調用f(), 此時java不知道應該調用那一個
public void f(){
}
public int f(){
return 1;
}

2. 只有當需要明確指出對當前對象的引用時, 才需要使用this關鍵字,例如, 當需要返回對當前對象的引用時,就常常在return語句這樣寫:

public class Leaf {private int i = 0;public Leaf increment() {i++;return this;}void print() {System.out.println("i = " + i);}public static void main(String[] args) {Leaf x = new Leaf();x.increment().increment().increment().print();}} 
// 由於increment()通過this關鍵字返回了對當前對象的引用,所以很容易在一條語句對同一個對象執行多次操作


3. 為什麼需要finalize()方法?
把一個對象用完後就“棄之不顧”的做法並非總是安全的,當然,java有記憶體回收行程負責回收無用對象佔據的記憶體資源,但也有特殊情況:假定你的對象(並非使用new)獲得了一塊“特殊”的記憶體地區,由於記憶體回收行程只知道釋放那些經由new分配的記憶體,所以它不知道該如何釋放該對象的這塊“特殊”記憶體。為了應對這種情況,java允許在類中定義一個名為finalize()的方法。

不該將finalize()作為通用的清理方法。

之所以要有finalize(),是由於在分配記憶體時可能採用了類似C語言中的做法,而非Java中的通常做法,這種噢概念情況主要發生在使用“本地方法”的情況下,本地方法是一種在Java中調用非Java代碼的方式。

無論是“記憶體回收”還是“終結方法”都不保證一定會發生,如果Java虛擬機器並未面臨記憶體耗盡的情形,它是不會浪費時間
去執行記憶體回收以恢複記憶體的。

4.初始化順序:
在類的內部,變數定義的先後順序決定了初始化的順序,即使變數定義散佈於方法定義之間,他們仍然會在任何方法(包括構造器)被調用之前得到初始化。

class Window {Window(int marker) {print("Window(" + marker + ")");}}class House {Window w1 = new Window(1); // Before constructorHouse() {// Show that we're in the constructor:print("House()");w3 = new Window(33); // Reinitialize w3}Window w2 = new Window(2); // After constructorvoid f() {print("f()");}Window w3 = new Window(3); // At end}public class OrderOfInitialization {public static void main(String[] args) {House h = new House();h.f(); // Shows that construction is done}} /* Output:Window(1)Window(2)Window(3)House()Window(33)f()*///:~

5. 待用資料初始化:
class Bowl {Bowl(int marker) {print("Bowl(" + marker + ")");}void f1(int marker) {print("f1(" + marker + ")");}}class Table {static Bowl bowl1 = new Bowl(1);Table() {print("Table()");bowl2.f1(1);}void f2(int marker) {print("f2(" + marker + ")");}static Bowl bowl2 = new Bowl(2);}class Cupboard {Bowl bowl3 = new Bowl(3);static Bowl bowl4 = new Bowl(4);Cupboard() {print("Cupboard()");bowl4.f1(2);}void f3(int marker) {print("f3(" + marker + ")");}static Bowl bowl5 = new Bowl(5);}public class StaticInitialization {public static void main(String[] args) {print("Creating new Cupboard() in main");new Cupboard();print("Creating new Cupboard() in main");new Cupboard();table.f2(1);cupboard.f3(1);}static Table table = new Table();static Cupboard cupboard = new Cupboard();} /* Output:Bowl(1)Bowl(2)Table()f1(1)Bowl(4)Bowl(5)Bowl(3)Cupboard()f1(2)Creating new Cupboard() in mainBowl(3)Cupboard()f1(2)Creating new Cupboard() in mainBowl(3)Cupboard()f1(2)f2(1)f3(1)*///:~
初始化的順序是先靜態對象, 而後是非靜態對象,上面的類中要執行main()(靜態方法)必須載入StaticInitialization,然後其靜態域table和cupboard被初始化,這將導致它們對應的類也被載入,並且由於它們也都包含靜態Bowl對象,因此Bowl隨後也被載入。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.