2012-11-21 42 views
4

這是我的代碼。我設置爲null這個引用,那麼爲什麼它打印不是空集這個== null在java中,之後也執行爲什麼繼續?

Test.java

try 
    {     
    new Test().setNull(); 
    System.out.println("not null set"); 
    } 
catch (Exception e) 
    {//Catch exception if any 
    System.err.println("Error: " + e.getMessage()); 
    } 

    } 
    public void setNull() 
    { 
    setNull(this); 
    } 
    public void setNull(Object thisRef) 
    { 
     thisRef = null; 
    } 

輸出: 不是空集

+0

'null'不導致異常,變量可以是'null'。嘗試在您將'Test'對象設置爲'null'後調用一個方法,這會導致異常。 –

+0

另外,您只是操作傳入對象的本地引用。調用者仍然有參考,所以它不會變得無法訪問。你可以將對象的引用分解爲自己的唯一方法是直接賦值給'this',但是由於'this'是一個語言關鍵字,而不是一些奇怪的,隱含的隱藏成員,所以你不能用它作爲左邊一項任務。 – jpm

+0

@HunterMcMillen設置爲null不會導致「NPE」。即使在設置null之後,調用同一個類的另一個方法也不會通過NPE,因爲'this'不是'null'。請看我的例子。 – sunil

回答

1
  1. 您的代碼不完整。下次請發佈一個簡單的工作示例。

  2. 您將this作爲參數傳遞給setNull()。請記住,參數是通過Java中的值傳遞的。 thisRef初始化爲指向與此相同的實例,但當您重新指定nullthisRef時,this的值保持不變。

4

設置爲null參考不會拋出NPE,否則想想你會如何取消你的references? 此外,當您將null分配給任何引用時,只有該引用與它指向的對象分離。但其餘的參考文獻仍然存在。

對於如: -

MyClass obj1 = new MyClass(); 
MyClass obj2 = obj1; 

obj1 = null; // only obj1 is detached from object 
System.out.println(obj2); // obj2 still points to original object 

所以,當你調用你的方法: -

new Test().setNull(); 

參考的副本存儲在this(Java是沒有參考傳遞,而是它通過值通過引用,然後再次傳遞給另一個方法,所以再做一個副本,W那麼你設置爲空。但原始參考仍然指向該對象。

只有當您嘗試調用null引用上的任何方法或訪問任何對象屬性時,纔會引發NPE。

+0

你說「局部變量不會改變任何參數」,但是通過「this」引用的是這個局部變量嗎? – sunleo

+0

@sunleo ..當你傳遞this給你的Object thisRef。然後創建一個引用副本,指向與「this」相同的對象。所以,即使你引用這個引用 - thisRef再次指向null,調用方法中的this仍然指向那個對象。 –

+0

+1好解釋 –

11

Java不是通過引用調用。 Java不是通過引用調用的。 Java是不是..

分配到局部變量變化由主叫方提供的任何說法。永遠。

因此函數「什麼都不做」:它將null賦值給局部變量(也是一個參數)並返回。

+5

只是呼吸的人。只是呼吸。 – thedan

+0

你說「局部變量不會改變任何參數」,但是我通過這個參考是這個局部變量嗎? – sunleo

+1

@sunleo你不會在Java中更改引用_value_ - Java是pass_by_value。該引用不會更改堆中的位置。 EOS –

1

只是另一個解釋自己的例子:

package com.test; 

public class Test { 

    public void setNull() { 
     System.out.println("Before setting null : "+ this); 
     System.out.println("Going to set null"); 
     this.setNull(this); 
     System.out.println("'this' after setting null in caller method : "+this); 

     this.print();// make sure that 'this' is not null; 
    } 

    public void print() 
    { 
     System.out.println("Another method"); 
    } 

    public void setNull(Object thisRef) { 
     // here you are referring the 'this' object via a variable 'thisRef' 
     System.out.println("Inside setNull"); 
     System.out.println("thisRef value : " + thisRef); // same as 'this' 
     // nullifying the 'thisRef' 
     thisRef = null; 
     System.out.println("thisRef after nullifying : "+thisRef);// ofcourse 'thisRef' is null 
     System.out.println("'this' after setting null in method : "+this); // here the 'this' object will not be null 
    } 

    public static void main(String[] args) { 
     new Test().setNull(); 
    } 
} 

和控制檯輸出:

Before setting null : [email protected] 
Going to set null 
Inside setNull 
thisRef value : [email protected] 
thisRef after nullifying : null 
'this' after setting null in method : [email protected] 
'this' after setting null in caller method : [email protected] 
Another method 
+0

我終於檢查,然後我得到澄清我自己.... – sunleo

+0

有用的知道,你有點:) – sunil

相關問題