2009-12-30 40 views
-1

我有一個相當標準的創建模式,其中一類暴露的靜態方法,返回的自身情況,比如:爲什麼在Java中調用靜態方法沒有被創建?

public class MyClass { 

    private MyClass(/*parameter list*/) { 
     // internal construction 
    }  

    public static MyClass GetMyClass(/*parameter list*/) { 
     return new MyClass(/*parameter list*/); 
    } 
} 

... 

//this line wont break in the debugger and seemingly never gets called - why? 
MyClass inst = MyClass.GetMyClass(/*parameter list*/); 

然而,inst始終爲空。我不能斷開調用靜態方法的行,調試器會忽略它 - 發生了什麼?

編輯:謝謝你的建議。

  • 所有項目已被清理和重建(manully在NetBeans)
  • 我加入的靜態方法休息,沒有它不打。
  • 是的,上面的代碼被調用(最終)在一個Swing'FrameView'的構造函數中,雖然它肯定不應該在我調用它的位置,應該嗎?
  • 沒有異常吞嚥隨時隨地

側面說明,不是缺少class聲明(這是一個錯字)其他爲什麼這不是有效的Java?爲什麼這個顯然是 C#代碼?說明會比downvotes :)

編輯II更有幫助:該Params只是應該指出的參數的整個負載 - 對不起,如果這混淆任何人,我明明知道參數有類型聲明等,代碼上述的目的是作爲一個快速的簡寫,而不是一個完整的和(複雜)的實際樣品...

+2

現貨C#程序員:)請嘗試發佈有效的,編譯Java(這不是)。 – skaffman

+0

那個調用'GetMyClass'的行在哪裏?在一個構造函數中,靜態/最終的init塊可能在另一個類中?還值得注意的是,發佈的代碼非常C#,在大多數地方無法使用Java。 –

+0

您提供的代碼不是Java代碼,所以它很難幫助您... – delfuego

回答

1

幾個選項:

  • 一個異常被拋出,你會莫名其妙地失蹤
  • 您並未調試您認爲自己的代碼(即你的內置代碼與你的源代碼過時了)

後者是最有可能的一個,國際海事組織。

1

顯然你吞嚥異常的構造類似的內部:

try { 
    // Something. 
} catch (Exception e) { 
} 

你不應該這樣做。它使得調試和釘死根本原因更難太多。寧可throw它或至少做一個e.printStackTrace()。如果拋出並且由於某些原因不想使用throws子句,請考慮使用RuntimeException(或其中的一個子類)。例如。

try { 
    // Something. 
} catch (Exception e) { 
    throw new RuntimeException("Construction failed.", e); // Try to be more specific, e.g. IllegalArgumentException or so. Or just write robust code, i.e. nullchecks and so on. 
} 

或(但在我看來,你的情況不是很適用):

try { 
    // Something. 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
1

據我所知,你正在試圖做一個簡單的例子來說明你的問題,但是如果你添加相應的在你的示例代碼中輸入語句,然後編譯並執行你的預期。

但是,在您的原始代碼庫中,您可以簡單地將斷點放在靜態方法中,以查看它是否被調用。

也許一個簡單的問題,但你永遠不知道......你確定你正在運行你認爲你正在運行的代碼嗎?也就是說,是否所有的東西都是從最新的資源中重新編譯和構建的?

1

沒有什麼不妥:

MyClass inst = MyClass.GetMyClass(Params); 

這要看是什麼之前或該行的代碼之後。

1

開始做這個:

public class MyClass 
{ 
    private MyClass(/*parameter list*/) 
    { 
     System.out.println("entering MyClass(...)"); 
     // internal construction 
     System.out.println("leaving MyClass(...)"); 
    }  

    // Java uses lower case for method names - so get not Get 
    public static MyClass getMyClass(/*parameter list*/) 
    { 
     final MyClass foo; 

     System.out.println("entering getMyClass(...)"); 

     foo = new MyClass(...); 

     System.out.println("leaving getMyClass(...)"); 

     return (foo); 
    } 
} 

... 

MyClass inst = MyClass.getMyClass(/*parameter list*/); 

看看在調試器外部的代碼被調用。

如果您正趕上任何異常,起碼做到:

catch(final WhateverException ex) 
{ 
    // at the very least do this so you can see that the exception happens 
    ex.printStackTrace(); 
} 

避免受涼Throwable的,錯誤,異常和RuntimeException的。事實上,做這件事的最好方法是擺脫所有的catch語句,然後只爲編譯器告訴你必須添加的內容。

另一件事是你不要說MyClass inst = MyClass.getMyClass(/ 參數列表 /);從中被調用。這條線路完全有可能不會被擊中。

+0

不應該公共靜態MyClass getMyClass(/ *參數列表* /)返回foo? –

+0

是的,我修正了...... :-) – TofuBeer

1

你提到你是從FrameView的構造函數中調用它的,但我假設你正在討論該接口/對象的實現或擴展。我的推理是確保你不會遞歸調用構造函數。

0

我覺得爲什麼捕捉java.lang.Exception沒有捕捉到的問題是因爲它可能在這種情況下太具體。嘗試捕獲java.lang.Throwable,它將捕獲類似的錯誤java.lang.NoClassDefFoundError - 當您在某處丟失一個jar時,經常會出現這種錯誤。

相關問題